Skip to content

Commit

Permalink
test: add failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 21, 2023
1 parent fd6805d commit 6e47978
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 18 deletions.
36 changes: 28 additions & 8 deletions lib/helpers/regexes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe("replaceVersionInContent", () => {
);
});

it("should not replace the version outside of the project definition", () => {
expect(replaceVersionInContent('version: "0.0.0-dev"', "1.0.0")).toBe(
'version: "0.0.0-dev"',
);

expect(replaceVersionInContent('@version "0.0.0-dev"', "1.0.0")).toBe(
'@version "0.0.0-dev"',
);
});

it("should preserve indentation and newline", () => {
expect(
replaceVersionInContent(
Expand All @@ -43,9 +53,13 @@ describe("versionRegex", () => {
expect.assertions(validSemVers.length * 6);

for (let semVer of validSemVers) {
expect(`version:"${semVer}"`).toMatch(versionRegex);
expect(` version: "${semVer}" ,`).toMatch(versionRegex);
expect(` version: "${semVer}" ,`).toMatch(versionRegex);
expect(`${DEF_P_1}version:"${semVer}"${DEF_P_2}`).toMatch(versionRegex);
expect(`${DEF_P_1} version: "${semVer}" ,${DEF_P_2}`).toMatch(
versionRegex,
);
expect(`${DEF_P_1} version: "${semVer}" ,${DEF_P_2}`).toMatch(
versionRegex,
);

expect(`@version "${semVer}"`).toMatch(versionRegex);
expect(` @version "${semVer}"`).toMatch(versionRegex);
Expand All @@ -55,24 +69,30 @@ describe("versionRegex", () => {

it("should not match invalid values", () => {
// eslint-disable-next-line jest/prefer-expect-assertions
expect.assertions(validSemVers.length * 10 + invalidSemVers.length * 2);
expect.assertions(validSemVers.length * 12 + invalidSemVers.length * 2);

for (let semVer of validSemVers) {
expect(`version:"${semVer}"`).not.toMatch(versionRegex);
expect(` version: "${semVer}" ,`).not.toMatch(versionRegex);
expect(` version: "${semVer}" ,`).not.toMatch(versionRegex);

expect(`version: ${semVer}`).not.toMatch(versionRegex);
expect(`version "${semVer}"`).not.toMatch(versionRegex);
expect(`versin: "${semVer}",`).not.toMatch(versionRegex);
expect(`"~> ${semVer}"`).not.toMatch(versionRegex);
expect(`tag: "${semVer}",`).not.toMatch(versionRegex);

expect(`@version ${semVer}`).not.toMatch(versionRegex);
expect(`@version"${semVer}"`).not.toMatch(versionRegex);
expect(`@versin "${semVer}"`).not.toMatch(versionRegex);
expect(`"~> ${semVer}"`).not.toMatch(versionRegex);

expect(`@tag "${semVer}"`).not.toMatch(versionRegex);
expect(`tag: "${semVer}",`).not.toMatch(versionRegex);
expect(`"~> ${semVer}"`).not.toMatch(versionRegex);
}

for (let semVer of invalidSemVers) {
expect(`version: "${semVer}"`).not.toMatch(versionRegex);
expect(`${DEF_P_1}version: "${semVer}"${DEF_P_2}`).not.toMatch(
versionRegex,
);

expect(`@version "${semVer}"`).not.toMatch(versionRegex);
}
Expand Down
44 changes: 44 additions & 0 deletions tests/fixtures/mix-regular-trap.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule HelloWorld.MixProject do
use Mix.Project

def some_config do
[
app: :hello_world,
version: "1.2.3",
some_config_key: "some string"
]
end

def project do
[
app: :hello_world,
version: "{{VERSION}}",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

def some_other_config do
[
app: :hello_world,
version: "4.5.6",
some_other_config_key: "some other string"
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
15 changes: 8 additions & 7 deletions tests/helpers/create-test-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import { temporaryDirectory } from "tempy";
*
* @param {string | null} [version] initial version to set in mix.exs (empty if not provided)
* @param {boolean | null} [asAttribute] whether to set the version as a module attribute
* @param {"trap" | null} [suffix] optional mix fixture file suffix
* @returns {Project}
*/
export function createTestProject(version, asAttribute) {
export function createTestProject(version, asAttribute, suffix) {
const versionType = "-" + (asAttribute ? "attribute" : "regular");
const fixtureSuffix = suffix ? `-${suffix}` : "";

const cwd = temporaryDirectory();
const projectPath = path.resolve(cwd, "mix.exs");
const projectContent = fs
.readFileSync(
`./tests/fixtures/mix-${asAttribute ? "attribute" : "regular"}.exs`,
{
encoding: "utf-8",
},
)
.readFileSync(`./tests/fixtures/mix${versionType}${fixtureSuffix}.exs`, {
encoding: "utf-8",
})
.replace("{{VERSION}}", version ?? "");

fs.writeFileSync(projectPath, projectContent);
Expand Down
35 changes: 32 additions & 3 deletions tests/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ describe("prepare", () => {
}
});

it("should update version in mix.exs", async () => {
it("should update project version in mix.exs", async () => {
expect.assertions(4);

for (let asAttribute of [false, true]) {
const { cwd, path } = createTestProject("0.0.1-dev", asAttribute);
const { cwd, path } = createTestProject("0.0.0-dev", asAttribute);

await prepare(
{},
Expand All @@ -50,6 +50,35 @@ describe("prepare", () => {
const packageContent = fs.readFileSync(path, { encoding: "utf-8" });

expect(packageContent).toMatch(versionRegex);
expect(packageContent).not.toMatch(/0\.0\.0-dev/);
const { version } = readProjectVersion(packageContent);
expect(version).toBe("1.0.0");
}
});

it("should not update the version outside of the project definition in mix.exs", async () => {
expect.assertions(4);

for (let asAttribute of [false, true]) {
const { cwd, path } = createTestProject("0.0.0-dev", asAttribute, "trap");

await prepare(
{},
{
...context,
cwd,
nextRelease: { version: "1.0.0" },
},
);

const packageContent = fs.readFileSync(path, { encoding: "utf-8" });

// should still contain the versions in some_config and some_other_config
expect(packageContent).toMatch(/1\.2\.3/);
expect(packageContent).toMatch(/4\.5\.6/);

expect(packageContent).toMatch(versionRegex);
expect(packageContent).not.toMatch(/0\.0\.0-dev/);
const { version } = readProjectVersion(packageContent);
expect(version).toBe("1.0.0");
}
Expand Down Expand Up @@ -84,7 +113,7 @@ describe("prepare", () => {
expect.assertions(4);

for (let asAttribute of [false, true]) {
const { cwd } = createTestProject("0.0.1-dev", asAttribute);
const { cwd } = createTestProject("0.0.0-dev", asAttribute);

await prepare(
{},
Expand Down

0 comments on commit 6e47978

Please sign in to comment.