Skip to content

Commit 824e647

Browse files
committed
modernize property-based tests
1 parent d5597a4 commit 824e647

File tree

4 files changed

+241
-209
lines changed

4 files changed

+241
-209
lines changed
+83-91
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as tmp from "tmp"
2-
import * as fs from "fs-extra"
32
import * as path from "path"
43

54
import { spawnSafeSync } from "../src/spawnSafe"
@@ -9,32 +8,51 @@ import { parsePatch } from "../src/patch/parse"
98

109
import { TestCase, Files } from "./testCases"
1110

11+
jest.mock("fs-extra", () => {
12+
let workingFiles: Files
13+
14+
function setWorkingFiles(files: Files) {
15+
workingFiles = files
16+
}
17+
18+
function getWorkingFiles() {
19+
return workingFiles
20+
}
21+
22+
return {
23+
setWorkingFiles,
24+
getWorkingFiles,
25+
ensureDirSync: jest.fn(),
26+
readFileSync: jest.fn(path => getWorkingFiles()[path].contents),
27+
writeFileSync: jest.fn(
28+
(path: string, contents: string, opts?: { mode?: number }) => {
29+
getWorkingFiles()[path] = {
30+
contents,
31+
mode: opts && typeof opts.mode === "number" ? opts.mode : 0o644,
32+
}
33+
},
34+
),
35+
unlinkSync: jest.fn(path => delete getWorkingFiles()[path]),
36+
moveSync: jest.fn((from, to) => {
37+
getWorkingFiles()[to] = getWorkingFiles()[from]
38+
delete getWorkingFiles()[from]
39+
}),
40+
}
41+
})
42+
1243
function writeFiles(cwd: string, files: Files): void {
44+
const mkdirpSync = require("fs-extra/lib/mkdirs/index.js").mkdirpSync
45+
const writeFileSync = require("fs").writeFileSync
1346
Object.keys(files).forEach(filePath => {
1447
if (!filePath.startsWith(".git/")) {
15-
fs.mkdirpSync(path.join(cwd, path.dirname(filePath)))
16-
fs.writeFileSync(path.join(cwd, filePath), files[filePath].contents, {
48+
mkdirpSync(path.join(cwd, path.dirname(filePath)))
49+
writeFileSync(path.join(cwd, filePath), files[filePath].contents, {
1750
mode: files[filePath].mode,
1851
})
1952
}
2053
})
2154
}
2255

23-
let workingFiles: Files
24-
25-
function setWorkingFiles(files: Files) {
26-
workingFiles = files
27-
}
28-
29-
function getWorkingFiles() {
30-
return workingFiles
31-
}
32-
33-
const properReadFileSync = fs.readFileSync
34-
const properWriteFileSync = fs.writeFileSync
35-
const properUnlinkSync = fs.unlinkSync
36-
const properMoveSync = fs.moveSync
37-
3856
function removeLeadingSpaceOnBlankLines(patchFileContents: string): string {
3957
return patchFileContents
4058
.split("\n")
@@ -43,104 +61,78 @@ function removeLeadingSpaceOnBlankLines(patchFileContents: string): string {
4361
}
4462

4563
export function executeTestCase(testCase: TestCase) {
64+
const fs = require("fs-extra")
65+
4666
function reportingFailures(f: () => void): void {
4767
try {
4868
f()
4969
} catch (e) {
5070
console.error("TEST CASE FAILED", {
5171
testCase,
52-
workingFiles: getWorkingFiles(),
72+
workingFiles: fs.getWorkingFiles(),
5373
})
5474
throw e
5575
}
5676
}
5777

58-
describe("the test case", () => {
59-
beforeEach(() => {
60-
;(fs as any).readFileSync = jest.fn(
61-
path => getWorkingFiles()[path].contents,
62-
)
63-
;(fs as any).writeFileSync = jest.fn(
64-
(path: string, contents: string, opts?: { mode?: number }) => {
65-
getWorkingFiles()[path] = {
66-
contents,
67-
mode: opts && typeof opts.mode === "number" ? opts.mode : 0o644,
68-
}
69-
},
70-
)
71-
;(fs as any).unlinkSync = jest.fn(path => delete getWorkingFiles()[path])
72-
;(fs as any).moveSync = jest.fn((from, to) => {
73-
getWorkingFiles()[to] = getWorkingFiles()[from]
74-
delete getWorkingFiles()[from]
75-
})
76-
})
78+
const tmpDir = tmp.dirSync({ unsafeCleanup: true, mode: 0o100777 })
7779

78-
afterEach(() => {
79-
;(fs as any).readFileSync = properReadFileSync
80-
;(fs as any).writeFileSync = properWriteFileSync
81-
;(fs as any).unlinkSync = properUnlinkSync
82-
;(fs as any).moveSync = properMoveSync
83-
})
80+
spawnSafeSync("git", ["init"], { cwd: tmpDir.name })
8481

85-
const tmpDir = tmp.dirSync({ unsafeCleanup: true, mode: 0o100777 })
82+
writeFiles(tmpDir.name, testCase.cleanFiles)
8683

87-
spawnSafeSync("git", ["init"], { cwd: tmpDir.name })
84+
spawnSafeSync("git", ["add", "-A"], { cwd: tmpDir.name })
85+
spawnSafeSync("git", ["commit", "-m", "blah"], {
86+
cwd: tmpDir.name,
87+
})
88+
spawnSafeSync("git", ["rm", "-rf", "*"], {
89+
cwd: tmpDir.name,
90+
})
8891

89-
writeFiles(tmpDir.name, testCase.cleanFiles)
92+
writeFiles(tmpDir.name, testCase.modifiedFiles)
93+
spawnSafeSync("git", ["add", "-A"], { cwd: tmpDir.name })
9094

91-
spawnSafeSync("git", ["add", "-A"], { cwd: tmpDir.name })
92-
spawnSafeSync("git", ["commit", "-m", "blah"], {
95+
const patchResult = spawnSafeSync(
96+
"git",
97+
["diff", "--color=never", "--cached"],
98+
{
9399
cwd: tmpDir.name,
94-
})
95-
spawnSafeSync("git", ["rm", "-rf", "*"], {
96-
cwd: tmpDir.name,
97-
})
100+
logStdErrOnError: true,
101+
throwOnError: true,
102+
},
103+
)
98104

99-
writeFiles(tmpDir.name, testCase.modifiedFiles)
100-
spawnSafeSync("git", ["add", "-A"], { cwd: tmpDir.name })
105+
const patchFileContents = patchResult.stdout.toString()
101106

102-
const patchResult = spawnSafeSync(
103-
"git",
104-
["diff", "--color=never", "--cached"],
105-
{
106-
cwd: tmpDir.name,
107-
logStdErrOnError: true,
108-
throwOnError: true,
109-
},
110-
)
107+
const patchFileContentsWithBlankLines = removeLeadingSpaceOnBlankLines(
108+
patchFileContents,
109+
)
111110

112-
const patchFileContents = patchResult.stdout.toString()
113-
114-
const patchFileContentsWithBlankLines = removeLeadingSpaceOnBlankLines(
115-
patchFileContents,
116-
)
117-
118-
it("looks the same whether parsed with blank lines or not", () => {
119-
reportingFailures(() => {
120-
expect(parsePatch(patchFileContents)).toEqual(
121-
parsePatch(patchFileContentsWithBlankLines),
122-
)
123-
})
111+
it("looks the same whether parsed with blank lines or not", () => {
112+
reportingFailures(() => {
113+
expect(parsePatch(patchFileContents)).toEqual(
114+
parsePatch(patchFileContentsWithBlankLines),
115+
)
124116
})
117+
})
125118

126-
// console.log(patchFileContents)
119+
// console.log(patchFileContents)
127120

128-
it("works forwards", () => {
129-
setWorkingFiles({ ...testCase.cleanFiles })
130-
reportingFailures(() => {
131-
const effects = patch(patchFileContents)
132-
executeEffects(effects)
133-
expect(getWorkingFiles()).toEqual(testCase.modifiedFiles)
134-
})
121+
it("works forwards", () => {
122+
fs.setWorkingFiles({ ...testCase.cleanFiles })
123+
reportingFailures(() => {
124+
const effects = patch(patchFileContents)
125+
executeEffects(effects)
126+
expect(fs.getWorkingFiles()).toEqual(testCase.modifiedFiles)
135127
})
128+
})
136129

137-
it("works backwards", () => {
138-
setWorkingFiles({ ...testCase.modifiedFiles })
139-
reportingFailures(() => {
140-
const result = patch(patchFileContents, { reverse: true })
141-
executeEffects(result)
142-
expect(getWorkingFiles()).toEqual(testCase.cleanFiles)
143-
})
130+
it("works backwards", () => {
131+
fs.setWorkingFiles({ ...testCase.modifiedFiles })
132+
reportingFailures(() => {
133+
const result = patch(patchFileContents, { reverse: true })
134+
executeEffects(result)
135+
expect(fs.getWorkingFiles()).toEqual(testCase.cleanFiles)
144136
})
145137
})
146138
}

property-based-tests/generativeTests.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { executeTestCase } from "./executeTestCase"
33

44
describe("property based tests", () => {
55
for (let i = 0; i < 200; i++) {
6-
executeTestCase(generateTestCase())
6+
describe(`${i}`, () => {
7+
executeTestCase(generateTestCase())
8+
})
79
}
810
})

0 commit comments

Comments
 (0)