-
-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathrunIntegrationTest.ts
More file actions
111 lines (98 loc) Β· 3.05 KB
/
Copy pathrunIntegrationTest.ts
File metadata and controls
111 lines (98 loc) Β· 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as fs from "fs"
import { join as pathJoin } from "path"
import { join, resolve } from "../src/path"
import * as tmp from "tmp"
import { spawnSafeSync } from "../src/spawnSafe"
import { resolveRelativeFileDependencies } from "../src/resolveRelativeFileDependencies"
function copyDirSync(src: string, dest: string) {
fs.mkdirSync(dest, { recursive: true })
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = pathJoin(src, entry.name)
const destPath = pathJoin(dest, entry.name)
if (entry.isDirectory()) {
copyDirSync(srcPath, destPath)
} else {
fs.copyFileSync(srcPath, destPath)
}
}
}
export const patchPackageTarballPath = resolve(
fs
.readdirSync(".")
.filter((nm: string) => nm.match(/^patch-package\.test\.\d+\.tgz$/))[0],
)
export function runIntegrationTest({
projectName,
shouldProduceSnapshots,
}: {
projectName: string
shouldProduceSnapshots: boolean
}) {
describe(`Test ${projectName}:`, () => {
const tmpDir = tmp.dirSync({ unsafeCleanup: true })
copyDirSync(join(__dirname, projectName), tmpDir.name)
// remove node_modules folder when running locally, to avoid leaking state from source dir
fs.rmSync(join(tmpDir.name, "node_modules"), {
recursive: true,
force: true,
})
const packageJson = require(join(tmpDir.name, "package.json"))
packageJson.dependencies = resolveRelativeFileDependencies(
join(__dirname, projectName),
packageJson.dependencies,
)
fs.writeFileSync(
join(tmpDir.name, "package.json"),
JSON.stringify(packageJson),
)
const result = spawnSafeSync(
`./${projectName}.sh`,
[patchPackageTarballPath],
{
cwd: tmpDir.name,
throwOnError: false,
env: {
...process.env,
PATCH_PACKAGE_INTEGRATION_TEST: "1",
},
shell: true,
},
)
it("should exit with 0 status", () => {
expect(result.status).toBe(0)
})
const output = result.stdout.toString() + "\n" + result.stderr.toString()
if (result.status !== 0) {
console.log(output)
}
it("should produce output", () => {
expect(output.trim()).toBeTruthy()
})
const snapshots = output.match(/SNAPSHOT: ?([\s\S]*?)END SNAPSHOT/g)
if (shouldProduceSnapshots) {
it("should produce some snapshots", () => {
expect(snapshots && snapshots.length).toBeTruthy()
})
if (snapshots) {
snapshots.forEach((snapshot, i) => {
const snapshotDescriptionMatch = snapshot.match(/SNAPSHOT: (.*)/)
if (snapshotDescriptionMatch) {
it(
`${i.toString().padStart(2, "0")}: ` +
snapshotDescriptionMatch[1],
() => {
expect(snapshot).toMatchSnapshot()
},
)
} else {
throw new Error("bad snapshot format")
}
})
}
} else {
it("should not produce any snapshots", () => {
expect(snapshots && snapshots.length).toBeFalsy()
})
}
})
}