1
|
|
|
import rewrite = require("./rewrite") |
2
|
|
|
import {resolve} from "path" |
3
|
|
|
import {readFileSync, appendFileSync, statSync, writeFileSync, existsSync, unlinkSync} from "fs" |
4
|
|
|
|
5
|
|
|
import { platform } from "os" |
6
|
|
|
|
7
|
|
|
const osBasedAssertion = platform() === "darwin" ? "toBeGreaterThan" : "toBeGreaterThanOrEqual" |
8
|
|
|
|
9
|
|
|
const eol = "\n" |
10
|
|
|
, ctx = (fileName: string) => { |
11
|
|
|
const filePath = resolve(__dirname, "rewrite.test", fileName) |
12
|
|
|
, modified = () => statSync(filePath).mtimeMs |
13
|
|
|
, original = readFileSync(filePath).toString().split(eol) |
14
|
|
|
, write = (content: string[]) => writeFileSync(filePath, content.join(eol)) |
15
|
|
|
|
16
|
|
|
afterEach(() => write(original)) |
17
|
|
|
|
18
|
|
|
return { |
19
|
|
|
filePath, |
20
|
|
|
original, |
21
|
|
|
modified, |
22
|
|
|
write |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
it("not exists - to write", async () => { |
27
|
|
|
const filename = "rewrite.txt"; |
28
|
|
|
expect(existsSync(filename)).toBe(false) |
29
|
|
|
await rewrite("rewrite.txt", ["whatever"], "") |
30
|
|
|
expect(existsSync(filename)).toBe(true) |
31
|
|
|
unlinkSync(filename) |
32
|
|
|
}) |
33
|
|
|
|
34
|
|
|
describe("with_last_new_line", () => { |
35
|
|
|
const {filePath, original, modified, write} = ctx("with_last_new_line.txt") |
36
|
|
|
|
37
|
|
|
it("same => No rewrite", async () => { |
38
|
|
|
const m = modified() |
39
|
|
|
await rewrite(filePath, original, eol) |
40
|
|
|
expect(modified()).toBe(m) |
41
|
|
|
}) |
42
|
|
|
|
43
|
|
|
it("append => Rewrite", async () => { |
44
|
|
|
const m = modified() |
45
|
|
|
await rewrite(filePath, original.concat(""), eol) |
46
|
|
|
expect(modified())[osBasedAssertion](m) |
47
|
|
|
}) |
48
|
|
|
|
49
|
|
|
it("appended => Rewrite", async () => { |
50
|
|
|
appendFileSync(filePath, "\n") |
51
|
|
|
const m = modified() |
52
|
|
|
await rewrite(filePath, original, eol) |
53
|
|
|
expect(modified())[osBasedAssertion](m) |
54
|
|
|
}) |
55
|
|
|
|
56
|
|
|
it("detach => Rewrite", async () => { |
57
|
|
|
const m = modified() |
58
|
|
|
await rewrite(filePath, original.slice(-1), eol) |
59
|
|
|
expect(modified())[osBasedAssertion](m) |
60
|
|
|
}) |
61
|
|
|
|
62
|
|
|
it("detached => Rewrite", async () => { |
63
|
|
|
write(original.slice(-1)) |
64
|
|
|
const m = modified() |
65
|
|
|
await rewrite(filePath, original, eol) |
66
|
|
|
expect(modified())[osBasedAssertion](m) |
67
|
|
|
}) |
68
|
|
|
}) |
69
|
|
|
|
70
|
|
|
describe("without_last_new_line", () => { |
71
|
|
|
const {filePath, original, modified, write} = ctx("without_last_new_line.txt") |
72
|
|
|
|
73
|
|
|
it("same => No rewrite", async () => { |
74
|
|
|
const m = modified() |
75
|
|
|
await rewrite(filePath, original, eol) |
76
|
|
|
expect(modified()).toBe(m) |
77
|
|
|
}) |
78
|
|
|
|
79
|
|
|
it("appended => No rewrite", async () => { |
80
|
|
|
const m = modified() |
81
|
|
|
await rewrite(filePath, original.concat(""), eol) |
82
|
|
|
expect(modified()).toBe(m) |
83
|
|
|
}) |
84
|
|
|
|
85
|
|
|
it("appended => No rewrite", async () => { |
86
|
|
|
appendFileSync(filePath, "\n") |
87
|
|
|
const m = modified() |
88
|
|
|
await rewrite(filePath, original, eol) |
89
|
|
|
expect(modified()).toBe(m) |
90
|
|
|
}) |
91
|
|
|
|
92
|
|
|
it("detach => Rewrite", async () => { |
93
|
|
|
const m = modified() |
94
|
|
|
await rewrite(filePath, original.slice(-1), eol) |
95
|
|
|
expect(modified())[osBasedAssertion](m) |
96
|
|
|
}) |
97
|
|
|
|
98
|
|
|
it("detached => Rewrite", async () => { |
99
|
|
|
write(original.slice(-1)) |
100
|
|
|
const m = modified() |
101
|
|
|
await rewrite(filePath, original, eol) |
102
|
|
|
expect(modified())[osBasedAssertion](m) |
103
|
|
|
}) |
104
|
|
|
}) |
105
|
|
|
|