-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: version and documentation (#33)
* fix: version regex * feat: add tests for regex and readAndWrite fn * fix: publish v2-haddock
- Loading branch information
Showing
4 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cabal-version: 1.12 | ||
version: 0.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,113 @@ | ||
import { expect } from "@assertive-ts/core"; | ||
|
||
import { readAndWriteNewCabal } from "../../src/prepare"; | ||
import { readAndWriteNewCabal, VERSION_PATTERN } from "../../src/prepare"; | ||
|
||
import { readFile, writeFile } from "fs/promises"; | ||
|
||
const fakeCabalPath = "./test/fixtures/test-1-package.cabal"; | ||
const fakeNewVersion = "0.0.7"; | ||
const cabalContent = "name: test-1-package\nversion: 0.0.1"; | ||
const fakeVersionCabalPath = "./test/fixtures/test-2-package.cabal"; | ||
const cabalVersionContent = "cabal-version: 1.12\nversion: 0.0.1"; | ||
|
||
describe("readAndWriteNewCabal", () => { | ||
afterEach(async () => { | ||
await writeFile(fakeCabalPath, cabalContent, "utf8"); | ||
}); | ||
context("when cabal file has name and version", () => { | ||
afterEach(async () => { | ||
await writeFile(fakeCabalPath, cabalContent, "utf8"); | ||
}); | ||
|
||
context("when version is semantic", () => { | ||
it("updates the version in the cabal file fixture", async () => { | ||
await readAndWriteNewCabal(fakeCabalPath, fakeNewVersion); | ||
context("when version is semantic", () => { | ||
it("updates the version in the cabal file fixture", async () => { | ||
await readAndWriteNewCabal(fakeCabalPath, fakeNewVersion); | ||
|
||
const modifiedContents = await readFile(fakeCabalPath, "utf8"); | ||
const modifiedContents = await readFile(fakeCabalPath, "utf8"); | ||
|
||
expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.7"); | ||
expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.7"); | ||
}); | ||
}); | ||
|
||
context("when version is not semantic", () => { | ||
it("updates the version in the cabal file fixture", async () => { | ||
const versionPrefix = "0."; | ||
await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion); | ||
|
||
const modifiedContents = await readFile(fakeCabalPath, "utf8"); | ||
|
||
expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.0.7"); | ||
}); | ||
}); | ||
}); | ||
|
||
context("when version is not semantic", () => { | ||
it("updates the version in the cabal file fixture", async () => { | ||
context("when cabal file has cabal-version and version", () => { | ||
afterEach(async () => { | ||
await writeFile(fakeVersionCabalPath, cabalVersionContent, "utf8"); | ||
}); | ||
|
||
it("updates the right version in the cabal file fixture", async () => { | ||
await readAndWriteNewCabal(fakeVersionCabalPath, fakeNewVersion); | ||
|
||
const modifiedContents = await readFile(fakeVersionCabalPath, "utf8"); | ||
|
||
expect(modifiedContents).toBeEqual("cabal-version: 1.12\nversion: 0.0.7"); | ||
}); | ||
|
||
it("updates the version in the cabal file fixture when version is not semantic", async () => { | ||
const versionPrefix = "0."; | ||
await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion); | ||
await readAndWriteNewCabal(fakeVersionCabalPath, versionPrefix + fakeNewVersion); | ||
|
||
const modifiedContents = await readFile(fakeCabalPath, "utf8"); | ||
const modifiedContents = await readFile(fakeVersionCabalPath, "utf8"); | ||
|
||
expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.0.7"); | ||
expect(modifiedContents).toBeEqual("cabal-version: 1.12\nversion: 0.0.0.7"); | ||
}); | ||
}); | ||
}); | ||
|
||
it("updates the version in the cabal file fixture when version is not semantic", async () => { | ||
const versionPrefix = "0."; | ||
await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion); | ||
describe("VERSION_PATTERN", () => { | ||
it("matches a valid version of strings", () => { | ||
const validStrings = ["version: 1.0.0", " version: 2.3.4", "version: 3.0.0-alpha", "version: 4.2.0-beta.1"]; | ||
|
||
const modifiedContents = await readFile(fakeCabalPath, "utf8"); | ||
validStrings.forEach(str => { | ||
const match = str.match(VERSION_PATTERN); | ||
expect(match).not.toBeNull(); | ||
if (match && match[1]) { | ||
expect(match[1]).not.toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
it("does not match invalid version strings", () => { | ||
const invalidStrings = ["version 1.0.0", "ver: 2.3.4", "version:", "version:"]; | ||
|
||
invalidStrings.forEach(str => { | ||
const match = str.match(VERSION_PATTERN); | ||
expect(match).toBeNull(); | ||
}); | ||
}); | ||
|
||
expect(modifiedContents).toBeEqual("name: test-1-package\nversion: 0.0.0.7"); | ||
it("matches version strings with extra spaces", () => { | ||
const validStrings = [" version: 1.0.0", "\tversion: 2.3.4", " \t version: 3.0.0-alpha"]; | ||
|
||
validStrings.forEach(str => { | ||
const match = str.match(VERSION_PATTERN); | ||
expect(match).not.toBeNull(); | ||
if (match && match[1]) { | ||
expect(match[1]).not.toBeUndefined(); | ||
} | ||
}); | ||
}); | ||
|
||
it("captures the correct version value", () => { | ||
const testCases = [ | ||
{ expected: "1.0.0", input: "version: 1.0.0" }, | ||
{ expected: "2.3.4", input: " version: 2.3.4" }, | ||
]; | ||
|
||
testCases.forEach(({ expected, input }) => { | ||
const match = input.match(VERSION_PATTERN); | ||
expect(match).not.toBeNull(); | ||
if (match && match[1]) { | ||
expect(match[1]).toBeEqual(expected); | ||
} | ||
}); | ||
}); | ||
}); |