diff --git a/lib/helpers/regexes.spec.js b/lib/helpers/regexes.spec.js index c026b09..af4f87c 100644 --- a/lib/helpers/regexes.spec.js +++ b/lib/helpers/regexes.spec.js @@ -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( @@ -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); @@ -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); } diff --git a/tests/fixtures/mix-regular-trap.exs b/tests/fixtures/mix-regular-trap.exs new file mode 100644 index 0000000..cbcae02 --- /dev/null +++ b/tests/fixtures/mix-regular-trap.exs @@ -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 diff --git a/tests/helpers/create-test-project.js b/tests/helpers/create-test-project.js index f896e5d..b838df2 100644 --- a/tests/helpers/create-test-project.js +++ b/tests/helpers/create-test-project.js @@ -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); diff --git a/tests/prepare.test.js b/tests/prepare.test.js index de49b66..b575624 100644 --- a/tests/prepare.test.js +++ b/tests/prepare.test.js @@ -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( {}, @@ -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"); } @@ -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( {},