Skip to content

Commit 547f8ca

Browse files
authored
Add Windows test in CI (#666)
* Add Windows test in CI * Fix CLI test in Windows * Resolve local Windows paths better
1 parent f17ca91 commit 547f8ca

File tree

8 files changed

+55
-194
lines changed

8 files changed

+55
-194
lines changed

.github/workflows/pull_request.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ name: branch
33
on: [pull_request]
44

55
jobs:
6-
build:
7-
runs-on: ubuntu-latest
8-
steps:
9-
- uses: actions/checkout@v2
10-
- uses: actions/setup-node@v2
11-
- run: npm ci
12-
- run: npm run build
136
lint:
147
runs-on: ubuntu-latest
158
steps:
@@ -18,10 +11,11 @@ jobs:
1811
- run: npm ci
1912
- run: npm run lint
2013
test:
21-
runs-on: ubuntu-latest
2214
strategy:
2315
matrix:
2416
node-version: [12.x, 14.x, 16.x]
17+
os: [ubuntu-latest, windows-latest]
18+
runs-on: ${{ matrix.os }}
2519
steps:
2620
- uses: actions/checkout@v2
2721
- uses: actions/setup-node@v2

package-lock.json

Lines changed: 2 additions & 144 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"mime": "^2.5.2",
6262
"node-fetch": "^2.6.1",
6363
"prettier": "^2.3.1",
64+
"slash": "^3.0.0",
6465
"tiny-glob": "^0.2.9"
6566
},
6667
"devDependencies": {

src/load.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import { URL } from "url";
44
import fetch, { Headers } from "node-fetch";
5+
import slash from "slash";
56
import mime from "mime";
67
import yaml from "js-yaml";
78
import { GlobalContext } from "./types";
@@ -36,7 +37,9 @@ export function resolveSchema(url: string): URL {
3637
}
3738

3839
// option 2: local
39-
const localPath = path.isAbsolute(url) ? new URL("", `file://${url}`) : new URL(url, `file://${process.cwd()}/`); // if absolute path is provided use that; otherwise search cwd\
40+
const localPath = path.isAbsolute(url)
41+
? new URL("", `file://${slash(url)}`)
42+
: new URL(url, `file://${slash(process.cwd())}/`); // if absolute path is provided use that; otherwise search cwd\
4043
if (!fs.existsSync(localPath)) {
4144
throw new Error(`Could not locate ${url}`);
4245
} else if (fs.statSync(localPath).isDirectory()) {
@@ -108,7 +111,9 @@ export default async function load(schemaURL: URL, options: LoadOptions): Promis
108111
if (refURL) {
109112
// load $refs (only if new) and merge subschemas with top-level schema
110113
const nextURL =
111-
refURL.startsWith("http://") || refURL.startsWith("https://") ? new URL(refURL) : new URL(refURL, schemaURL);
114+
refURL.startsWith("http://") || refURL.startsWith("https://")
115+
? new URL(refURL)
116+
: new URL(slash(refURL), schemaURL);
112117
refPromises.push(
113118
load(nextURL, options).then((subschemas) => {
114119
for (const subschemaURL of Object.keys(subschemas)) {

tests/bin/cli.test.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
import fs from "fs";
22
import path from "path";
33
import { execSync } from "child_process";
4+
import { sanitizeLB } from "../test-utils";
45

56
// Note(drew): OpenAPI support is already well-tested in v2/index.test.ts and
67
// v3/index.test.ts. So this file is mainly for testing other flags.
78

9+
const cmd = `node ../../bin/cli.js`;
10+
811
describe("cli", () => {
912
it("--prettier-config (JSON)", async () => {
10-
execSync(
11-
`../../bin/cli.js specs/petstore.yaml -o generated/prettier-json.ts --prettier-config fixtures/.prettierrc`,
12-
{ cwd: __dirname }
13-
);
13+
execSync(`${cmd} specs/petstore.yaml -o generated/prettier-json.ts --prettier-config fixtures/.prettierrc`, {
14+
cwd: __dirname,
15+
});
1416
const [generated, expected] = await Promise.all([
1517
fs.promises.readFile(path.join(__dirname, "generated", "prettier-json.ts"), "utf8"),
1618
fs.promises.readFile(path.join(__dirname, "expected", "prettier-json.ts"), "utf8"),
1719
]);
18-
expect(generated).toBe(expected);
20+
expect(generated).toBe(sanitizeLB(expected));
1921
});
2022

2123
it("--prettier-config (.js)", async () => {
22-
execSync(
23-
`../../bin/cli.js specs/petstore.yaml -o generated/prettier-js.ts --prettier-config fixtures/prettier.config.js`,
24-
{ cwd: __dirname }
25-
);
24+
execSync(`${cmd} specs/petstore.yaml -o generated/prettier-js.ts --prettier-config fixtures/prettier.config.js`, {
25+
cwd: __dirname,
26+
});
2627
const [generated, expected] = await Promise.all([
2728
fs.promises.readFile(path.join(__dirname, "generated", "prettier-js.ts"), "utf8"),
2829
fs.promises.readFile(path.join(__dirname, "expected", "prettier-js.ts"), "utf8"),
2930
]);
30-
expect(generated).toBe(expected);
31+
expect(generated).toBe(sanitizeLB(expected));
3132
});
3233

3334
it("stdout", async () => {
3435
const expected = fs.readFileSync(path.join(__dirname, "expected", "stdout.ts"), "utf8");
35-
const generated = execSync(`../../bin/cli.js specs/petstore.yaml`, { cwd: __dirname });
36-
expect(generated.toString("utf8")).toBe(expected);
36+
const generated = execSync(`${cmd} specs/petstore.yaml`, { cwd: __dirname });
37+
expect(generated.toString("utf8")).toBe(sanitizeLB(expected));
3738
});
3839

3940
it("supports glob paths", async () => {
40-
execSync(`../../bin/cli.js "specs/*.yaml" -o generated/`, { cwd: __dirname }); // Quotes are necessary because shells like zsh treats glob weirdly
41+
execSync(`${cmd} "specs/*.yaml" -o generated/`, { cwd: __dirname }); // Quotes are necessary because shells like zsh treats glob weirdly
4142
const [generatedPetstore, expectedPetstore, generatedManifold, expectedManifold] = await Promise.all([
4243
fs.promises.readFile(path.join(__dirname, "generated", "specs", "petstore.ts"), "utf8"),
4344
fs.promises.readFile(path.join(__dirname, "expected", "petstore.ts"), "utf8"),
4445
fs.promises.readFile(path.join(__dirname, "generated", "specs", "manifold.ts"), "utf8"),
4546
fs.promises.readFile(path.join(__dirname, "expected", "manifold.ts"), "utf8"),
4647
]);
47-
expect(generatedPetstore).toBe(expectedPetstore);
48-
expect(generatedManifold).toBe(expectedManifold);
48+
expect(generatedPetstore).toBe(sanitizeLB(expectedPetstore));
49+
expect(generatedManifold).toBe(sanitizeLB(expectedManifold));
4950
});
5051
});

tests/test-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function sanitizeLB(code: string): string {
2+
return code.replace(/\r\n/g, "\n");
3+
}

tests/v2/index.test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { execSync } from "child_process";
22
import fs from "fs";
33
import path from "path";
4+
import { sanitizeLB } from "../test-utils";
45

6+
const cmd = `node ../../bin/cli.js`;
57
const schemas = fs.readdirSync(path.join(__dirname, "specs"));
68

79
// simple snapshot tests with valid schemas to make sure it can generally parse & generate output
@@ -10,43 +12,40 @@ describe("cli", () => {
1012
const output = schema.replace(".yaml", ".ts");
1113

1214
it(`reads ${schema} spec (v2) from file`, async () => {
13-
execSync(`../../bin/cli.js specs/${schema} -o generated/${output} --prettier-config .prettierrc`, {
15+
execSync(`${cmd} specs/${schema} -o generated/${output} --prettier-config .prettierrc`, {
1416
cwd: __dirname,
1517
});
1618
const [generated, expected] = await Promise.all([
1719
fs.promises.readFile(path.join(__dirname, "generated", output), "utf8"),
1820
fs.promises.readFile(path.join(__dirname, "expected", output), "utf8"),
1921
]);
20-
expect(generated).toBe(expected);
22+
expect(generated).toBe(sanitizeLB(expected));
2123
});
2224

2325
it(`reads ${schema} spec (v2) from file (immutable types)`, async () => {
2426
const output = schema.replace(".yaml", ".immutable.ts");
2527

26-
execSync(
27-
`../../bin/cli.js specs/${schema} -o generated/${output} --prettier-config .prettierrc --immutable-types`,
28-
{
29-
cwd: __dirname,
30-
}
31-
);
28+
execSync(`${cmd} specs/${schema} -o generated/${output} --prettier-config .prettierrc --immutable-types`, {
29+
cwd: __dirname,
30+
});
3231

3332
const [generated, expected] = await Promise.all([
3433
fs.promises.readFile(path.join(__dirname, "generated", output), "utf8"),
3534
fs.promises.readFile(path.join(__dirname, "expected", output), "utf8"),
3635
]);
37-
expect(generated).toBe(expected);
36+
expect(generated).toBe(sanitizeLB(expected));
3837
});
3938
});
4039

4140
it("reads spec (v2) from remote resource", async () => {
4241
execSync(
43-
"../../bin/cli.js https://raw.githubusercontent.com/drwpow/openapi-typescript/main/tests/v2/specs/manifold.yaml -o generated/http.ts",
42+
`${cmd} https://raw.githubusercontent.com/drwpow/openapi-typescript/main/tests/v2/specs/manifold.yaml -o generated/http.ts`,
4443
{ cwd: __dirname }
4544
);
4645
const [generated, expected] = await Promise.all([
4746
fs.promises.readFile(path.join(__dirname, "generated", "http.ts"), "utf8"),
4847
fs.promises.readFile(path.join(__dirname, "expected", "http.ts"), "utf8"),
4948
]);
50-
expect(generated).toBe(expected);
49+
expect(generated).toBe(sanitizeLB(expected));
5150
});
5251
});

0 commit comments

Comments
 (0)