|
1 | 1 | import { expect } from "@assertive-ts/core";
|
2 | 2 |
|
3 |
| -import { readAndWriteNewCabal } from "../../src/prepare"; |
| 3 | +import { readAndWriteNewCabal, VERSION_PATTERN } from "../../src/prepare"; |
4 | 4 |
|
5 | 5 | import { readFile, writeFile } from "fs/promises";
|
6 | 6 |
|
7 | 7 | const fakeCabalPath = "./test/fixtures/test-1-package.cabal";
|
8 | 8 | const fakeNewVersion = "0.0.7";
|
9 | 9 | const cabalContent = "name: test-1-package\nversion: 0.0.1";
|
| 10 | +const fakeVersionCabalPath = "./test/fixtures/test-2-package.cabal"; |
| 11 | +const cabalVersionContent = "cabal-version: 1.12\nversion: 0.0.1"; |
10 | 12 |
|
11 | 13 | describe("readAndWriteNewCabal", () => {
|
12 |
| - afterEach(async () => { |
13 |
| - await writeFile(fakeCabalPath, cabalContent, "utf8"); |
14 |
| - }); |
| 14 | + context("when cabal file has name and version", () => { |
| 15 | + afterEach(async () => { |
| 16 | + await writeFile(fakeCabalPath, cabalContent, "utf8"); |
| 17 | + }); |
15 | 18 |
|
16 |
| - context("when version is semantic", () => { |
17 |
| - it("updates the version in the cabal file fixture", async () => { |
18 |
| - await readAndWriteNewCabal(fakeCabalPath, fakeNewVersion); |
| 19 | + context("when version is semantic", () => { |
| 20 | + it("updates the version in the cabal file fixture", async () => { |
| 21 | + await readAndWriteNewCabal(fakeCabalPath, fakeNewVersion); |
19 | 22 |
|
20 |
| - const modifiedContents = await readFile(fakeCabalPath, "utf8"); |
| 23 | + const modifiedContents = await readFile(fakeCabalPath, "utf8"); |
21 | 24 |
|
22 |
| - expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.7"); |
| 25 | + expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.7"); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + context("when version is not semantic", () => { |
| 30 | + it("updates the version in the cabal file fixture", async () => { |
| 31 | + const versionPrefix = "0."; |
| 32 | + await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion); |
| 33 | + |
| 34 | + const modifiedContents = await readFile(fakeCabalPath, "utf8"); |
| 35 | + |
| 36 | + expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.0.7"); |
| 37 | + }); |
23 | 38 | });
|
24 | 39 | });
|
25 | 40 |
|
26 |
| - context("when version is not semantic", () => { |
27 |
| - it("updates the version in the cabal file fixture", async () => { |
| 41 | + context("when cabal file has cabal-version and version", () => { |
| 42 | + afterEach(async () => { |
| 43 | + await writeFile(fakeVersionCabalPath, cabalVersionContent, "utf8"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("updates the right version in the cabal file fixture", async () => { |
| 47 | + await readAndWriteNewCabal(fakeVersionCabalPath, fakeNewVersion); |
| 48 | + |
| 49 | + const modifiedContents = await readFile(fakeVersionCabalPath, "utf8"); |
| 50 | + |
| 51 | + expect(modifiedContents).toBeEqual("cabal-version: 1.12\nversion: 0.0.7"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("updates the version in the cabal file fixture when version is not semantic", async () => { |
28 | 55 | const versionPrefix = "0.";
|
29 |
| - await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion); |
| 56 | + await readAndWriteNewCabal(fakeVersionCabalPath, versionPrefix + fakeNewVersion); |
30 | 57 |
|
31 |
| - const modifiedContents = await readFile(fakeCabalPath, "utf8"); |
| 58 | + const modifiedContents = await readFile(fakeVersionCabalPath, "utf8"); |
32 | 59 |
|
33 |
| - expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.0.7"); |
| 60 | + expect(modifiedContents).toBeEqual("cabal-version: 1.12\nversion: 0.0.0.7"); |
34 | 61 | });
|
35 | 62 | });
|
| 63 | +}); |
36 | 64 |
|
37 |
| - it("updates the version in the cabal file fixture when version is not semantic", async () => { |
38 |
| - const versionPrefix = "0."; |
39 |
| - await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion); |
| 65 | +describe("VERSION_PATTERN", () => { |
| 66 | + it("matches a valid version of strings", () => { |
| 67 | + const validStrings = ["version: 1.0.0", " version: 2.3.4", "version: 3.0.0-alpha", "version: 4.2.0-beta.1"]; |
40 | 68 |
|
41 |
| - const modifiedContents = await readFile(fakeCabalPath, "utf8"); |
| 69 | + validStrings.forEach(str => { |
| 70 | + const match = str.match(VERSION_PATTERN); |
| 71 | + expect(match).not.toBeNull(); |
| 72 | + if (match && match[1]) { |
| 73 | + expect(match[1]).not.toBeUndefined(); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("does not match invalid version strings", () => { |
| 79 | + const invalidStrings = ["version 1.0.0", "ver: 2.3.4", "version:", "version:"]; |
| 80 | + |
| 81 | + invalidStrings.forEach(str => { |
| 82 | + const match = str.match(VERSION_PATTERN); |
| 83 | + expect(match).toBeNull(); |
| 84 | + }); |
| 85 | + }); |
42 | 86 |
|
43 |
| - expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.0.7"); |
| 87 | + it("matches version strings with extra spaces", () => { |
| 88 | + const validStrings = [" version: 1.0.0", "\tversion: 2.3.4", " \t version: 3.0.0-alpha"]; |
| 89 | + |
| 90 | + validStrings.forEach(str => { |
| 91 | + const match = str.match(VERSION_PATTERN); |
| 92 | + expect(match).not.toBeNull(); |
| 93 | + if (match && match[1]) { |
| 94 | + expect(match[1]).not.toBeUndefined(); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("captures the correct version value", () => { |
| 100 | + const testCases = [ |
| 101 | + { expected: "1.0.0", input: "version: 1.0.0" }, |
| 102 | + { expected: "2.3.4", input: " version: 2.3.4" }, |
| 103 | + ]; |
| 104 | + |
| 105 | + testCases.forEach(({ expected, input }) => { |
| 106 | + const match = input.match(VERSION_PATTERN); |
| 107 | + expect(match).not.toBeNull(); |
| 108 | + if (match && match[1]) { |
| 109 | + expect(match[1]).toBeEqual(expected); |
| 110 | + } |
| 111 | + }); |
44 | 112 | });
|
45 | 113 | });
|
0 commit comments