Skip to content

Commit 832dd9b

Browse files
authored
fix: version and documentation (#33)
* fix: version regex * feat: add tests for regex and readAndWrite fn * fix: publish v2-haddock
1 parent 88a1cb5 commit 832dd9b

File tree

4 files changed

+94
-21
lines changed

4 files changed

+94
-21
lines changed

src/prepare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { lookupCabalFilename } from "./utils/prepare";
77
import { readFile, writeFile } from "fs/promises";
88
import { resolve } from "path";
99

10-
export const VERSION_PATTERN = /version:\s+(\S+)/;
10+
export const VERSION_PATTERN = /^\s*version:\s+(\S+)/m;
1111

1212
export const readAndWriteNewCabal = async (fullCabalPath: string, newVersion: string): Promise<void> => {
1313
const versionContents = await readFile(fullCabalPath, "utf8");

src/publish.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import fs from "fs";
88

99
export const HACKAGE_CANDIDATES_URL = "https://hackage.haskell.org/packages/candidates";
1010

11+
const V2_HADDOCK_COMMAND = "cabal v2-haddock --haddock-for-hackage --enable-documentation";
12+
1113
export const postReleaseCandidate = async (sdistPath: string, hackageToken?: string): Promise<number | undefined> => {
1214
try {
1315
const headers = {
@@ -70,7 +72,7 @@ export const publish = async (
7072
logger.log("Checking publishDocumentation plugin configuration: ", publishDocumentation);
7173
if (publishDocumentation) {
7274
logger.log("Generating documentation");
73-
const { error, output } = await runExecCommand("cabal haddock --haddock-for-hackage --enable-documentation");
75+
const { error, output } = await runExecCommand(V2_HADDOCK_COMMAND);
7476

7577
if (error) {
7678
logger.error(error);
@@ -82,6 +84,7 @@ export const publish = async (
8284
const docsSdistPath = `${realCwd}/dist-newstyle/${docsFilename}`;
8385
const docsUrl = `https://hackage.haskell.org/package/${packageName}-${versionPrefix}${version}/candidate/docs`;
8486

87+
logger.log("Publishing file: ", docsFilename, " from: ", docsSdistPath);
8588
const docStatus = await publishRCDocumentation(docsSdistPath, docsUrl, process.env.HACKAGE_TOKEN);
8689

8790
if (docStatus !== 200) {

test/fixtures/test-2-package.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cabal-version: 1.12
2+
version: 0.0.1

test/unit/prepare.test.ts

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,113 @@
11
import { expect } from "@assertive-ts/core";
22

3-
import { readAndWriteNewCabal } from "../../src/prepare";
3+
import { readAndWriteNewCabal, VERSION_PATTERN } from "../../src/prepare";
44

55
import { readFile, writeFile } from "fs/promises";
66

77
const fakeCabalPath = "./test/fixtures/test-1-package.cabal";
88
const fakeNewVersion = "0.0.7";
99
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";
1012

1113
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+
});
1518

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);
1922

20-
const modifiedContents = await readFile(fakeCabalPath, "utf8");
23+
const modifiedContents = await readFile(fakeCabalPath, "utf8");
2124

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+
});
2338
});
2439
});
2540

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 () => {
2855
const versionPrefix = "0.";
29-
await readAndWriteNewCabal(fakeCabalPath, versionPrefix + fakeNewVersion);
56+
await readAndWriteNewCabal(fakeVersionCabalPath, versionPrefix + fakeNewVersion);
3057

31-
const modifiedContents = await readFile(fakeCabalPath, "utf8");
58+
const modifiedContents = await readFile(fakeVersionCabalPath, "utf8");
3259

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");
3461
});
3562
});
63+
});
3664

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"];
4068

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+
});
4286

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+
});
44112
});
45113
});

0 commit comments

Comments
 (0)