This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.test.js
142 lines (131 loc) · 5.45 KB
/
loader.test.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import compiler from "./compiler";
import {
removeGeneratedFiles,
readFileAsync,
fileStatAsync,
existsAsync
} from "./helpers";
afterEach(removeGeneratedFiles);
it("should not affect file content", async () => {
const targetFileName = "./fixtures/foo.txt";
const status = await compiler(targetFileName, {
transmitRules: [{ test: /\/(.*$)/, targets: ["$1.null"] }]
});
const output = status.toJson().modules[0].source;
expect(output).toBe(await readFileAsync(targetFileName));
});
it("should not renew itself by default", async () => {
// to prevent infinite renewing
const targetFileName = "./fixtures/foo.txt";
const preStat = await fileStatAsync(targetFileName);
await compiler(targetFileName, {
transmitRules: [{ test: /\/(.*$)/, targets: ["$1"] }]
}); // identity mapping
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBe(preStat.ctime.getTime());
});
it("should not renew itself within multi targets", async () => {
// to prevent infinite renewing
const targetFileName = "./fixtures/foo.txt";
const preStat = await fileStatAsync(targetFileName);
await compiler(targetFileName, {
transmitRules: [{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] }]
});
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBe(preStat.ctime.getTime());
});
it("should not renew itself within multi rules", async () => {
// to prevent infinite renewing
const targetFileName = "./fixtures/foo.txt";
const preStat = await fileStatAsync(targetFileName);
await compiler(targetFileName, {
transmitRules: [
{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] },
{ test: /\/(.*$)/, targets: ["$1", "$1", "foo", "bar"] }
]
});
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBe(preStat.ctime.getTime());
});
it("should renew foo.md from foo.txt", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetFileName = "./fixtures/foo.md";
const preStat = await fileStatAsync(targetFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md"] }]
});
const postStat = await fileStatAsync(targetFileName);
expect(postStat.ctime.getTime()).toBeGreaterThan(preStat.ctime.getTime());
});
it("should renew foo.md and foo.asciidoc from foo.txt", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetMdFileName = "./fixtures/foo.md";
const targetAdFileName = "./fixtures/foo.asciidoc";
const preStatMd = await fileStatAsync(targetMdFileName);
const preStatAd = await fileStatAsync(targetAdFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md", ".asciidoc"] }]
});
const postStatMd = await fileStatAsync(targetMdFileName);
const postStatAd = await fileStatAsync(targetAdFileName);
expect(postStatMd.ctime.getTime()).toBeGreaterThan(preStatMd.ctime.getTime());
expect(postStatAd.ctime.getTime()).toBeGreaterThan(preStatAd.ctime.getTime());
});
it("should renew foo.md and foo.asciidoc and foo.rst from foo.txt", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetMdFileName = "./fixtures/foo.md";
const targetAdFileName = "./fixtures/foo.asciidoc";
const targetRstFileName = "./fixtures/foo.rst";
const preStatMd = await fileStatAsync(targetMdFileName);
const preStatAd = await fileStatAsync(targetAdFileName);
const preStatRst = await fileStatAsync(targetRstFileName);
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".md", ".asciidoc", ".rst"] }]
});
const postStatMd = await fileStatAsync(targetMdFileName);
const postStatAd = await fileStatAsync(targetAdFileName);
const postStatRst = await fileStatAsync(targetRstFileName);
expect(postStatMd.ctime.getTime()).toBeGreaterThan(preStatMd.ctime.getTime());
expect(postStatAd.ctime.getTime()).toBeGreaterThan(preStatAd.ctime.getTime());
expect(postStatRst.ctime.getTime()).toBeGreaterThan(
preStatRst.ctime.getTime()
);
});
it("should renew foo.md from foo.txt and bar.txt from foo.txt", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetFooFileName = "./fixtures/foo.md";
const targetBarFileName = "./fixtures/bar.txt";
const preStatFoo = await fileStatAsync(targetFooFileName);
const preStatBar = await fileStatAsync(targetBarFileName);
await compiler(sourceFileName, {
transmitRules: [
{ test: /(\.txt$)/, targets: [".md"] },
{ test: /(foo)/, targets: ["bar"] }
]
});
const postStatFoo = await fileStatAsync(targetFooFileName);
const postStatBar = await fileStatAsync(targetBarFileName);
expect(postStatFoo.ctime.getTime()).toBeGreaterThan(
preStatFoo.ctime.getTime()
);
expect(postStatBar.ctime.getTime()).toBeGreaterThan(
preStatBar.ctime.getTime()
);
});
it("should not create a new file by default", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetFileName = "./fixtures/foo.null";
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".null"] }]
});
expect(await existsAsync(targetFileName)).toBe(false);
});
it("should create a new file if option is set", async () => {
const sourceFileName = "./fixtures/foo.txt";
const targetFileName = "./fixtures/foo.null";
await compiler(sourceFileName, {
transmitRules: [{ test: /(\.txt$)/, targets: [".null"] }],
noCreate: false
});
expect(await existsAsync(targetFileName)).toBe(true);
});