|
1 | | -/// <reference path="..\harness.ts" /> |
2 | | -/// <reference path="..\..\compiler\commandLineParser.ts" /> |
3 | | - |
4 | | -namespace ts { |
5 | | - describe("parseConfigFileTextToJson", () => { |
6 | | - function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) { |
7 | | - const parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); |
8 | | - assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject)); |
9 | | - } |
10 | | - |
11 | | - function assertParseError(jsonText: string) { |
12 | | - const parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); |
13 | | - assert.isTrue(undefined === parsed.config); |
14 | | - assert.isTrue(undefined !== parsed.error); |
15 | | - } |
16 | | - |
17 | | - function assertParseErrorWithExcludesKeyword(jsonText: string) { |
18 | | - const parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); |
19 | | - const parsedCommand = ts.parseJsonConfigFileContent(parsed.config, ts.sys, "tests/cases/unittests"); |
20 | | - assert.isTrue(parsedCommand.errors && parsedCommand.errors.length === 1 && |
21 | | - parsedCommand.errors[0].code === ts.Diagnostics.Unknown_option_excludes_Did_you_mean_exclude.code); |
22 | | - } |
23 | | - |
24 | | - function assertParseFileList(jsonText: string, configFileName: string, basePath: string, allFileList: string[], expectedFileList: string[]) { |
25 | | - const json = JSON.parse(jsonText); |
26 | | - const host: ParseConfigHost = new Utils.MockParseConfigHost(basePath, true, allFileList); |
27 | | - const parsed = ts.parseJsonConfigFileContent(json, host, basePath, /*existingOptions*/ undefined, configFileName); |
28 | | - assert.isTrue(arrayIsEqualTo(parsed.fileNames.sort(), expectedFileList.sort())); |
29 | | - } |
30 | | - |
31 | | - it("returns empty config for file with only whitespaces", () => { |
32 | | - assertParseResult("", { config : {} }); |
33 | | - assertParseResult(" ", { config : {} }); |
34 | | - }); |
35 | | - |
36 | | - it("returns empty config for file with comments only", () => { |
37 | | - assertParseResult("// Comment", { config: {} }); |
38 | | - assertParseResult("/* Comment*/", { config: {} }); |
39 | | - }); |
40 | | - |
41 | | - it("returns empty config when config is empty object", () => { |
42 | | - assertParseResult("{}", { config: {} }); |
43 | | - }); |
44 | | - |
45 | | - it("returns config object without comments", () => { |
46 | | - assertParseResult( |
47 | | - `{ // Excluded files |
48 | | - "exclude": [ |
49 | | - // Exclude d.ts |
50 | | - "file.d.ts" |
51 | | - ] |
52 | | - }`, { config: { exclude: ["file.d.ts"] } }); |
53 | | - |
54 | | - assertParseResult( |
55 | | - `{ |
56 | | - /* Excluded |
57 | | - Files |
58 | | - */ |
59 | | - "exclude": [ |
60 | | - /* multiline comments can be in the middle of a line */"file.d.ts" |
61 | | - ] |
62 | | - }`, { config: { exclude: ["file.d.ts"] } }); |
63 | | - }); |
64 | | - |
65 | | - it("keeps string content untouched", () => { |
66 | | - assertParseResult( |
67 | | - `{ |
68 | | - "exclude": [ |
69 | | - "xx//file.d.ts" |
70 | | - ] |
71 | | - }`, { config: { exclude: ["xx//file.d.ts"] } }); |
72 | | - assertParseResult( |
73 | | - `{ |
74 | | - "exclude": [ |
75 | | - "xx/*file.d.ts*/" |
76 | | - ] |
77 | | - }`, { config: { exclude: ["xx/*file.d.ts*/"] } }); |
78 | | - }); |
79 | | - |
80 | | - it("handles escaped characters in strings correctly", () => { |
81 | | - assertParseResult( |
82 | | - `{ |
83 | | - "exclude": [ |
84 | | - "xx\\"//files" |
85 | | - ] |
86 | | - }`, { config: { exclude: ["xx\"//files"] } }); |
87 | | - |
88 | | - assertParseResult( |
89 | | - `{ |
90 | | - "exclude": [ |
91 | | - "xx\\\\" // end of line comment |
92 | | - ] |
93 | | - }`, { config: { exclude: ["xx\\"] } }); |
94 | | - }); |
95 | | - |
96 | | - it("returns object with error when json is invalid", () => { |
97 | | - assertParseError("invalid"); |
98 | | - }); |
99 | | - |
100 | | - it("returns object when users correctly specify library", () => { |
101 | | - assertParseResult( |
102 | | - `{ |
103 | | - "compilerOptions": { |
104 | | - "lib": ["es5"] |
105 | | - } |
106 | | - }`, { |
107 | | - config: { compilerOptions: { lib: ["es5"] } } |
108 | | - }); |
109 | | - |
110 | | - assertParseResult( |
111 | | - `{ |
112 | | - "compilerOptions": { |
113 | | - "lib": ["es5", "es6"] |
114 | | - } |
115 | | - }`, { |
116 | | - config: { compilerOptions: { lib: ["es5", "es6"] } } |
117 | | - }); |
118 | | - }); |
119 | | - |
120 | | - it("returns error when tsconfig have excludes", () => { |
121 | | - assertParseErrorWithExcludesKeyword( |
122 | | - `{ |
123 | | - "compilerOptions": { |
124 | | - "lib": ["es5"] |
125 | | - }, |
126 | | - "excludes": [ |
127 | | - "foge.ts" |
128 | | - ] |
129 | | - }`); |
130 | | - }); |
131 | | - |
132 | | - it("ignore dotted files and folders", () => { |
133 | | - assertParseFileList( |
134 | | - `{}`, |
135 | | - "tsconfig.json", |
136 | | - "/apath", |
137 | | - ["/apath/test.ts", "/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"], |
138 | | - ["/apath/test.ts"] |
139 | | - ); |
140 | | - }); |
141 | | - |
142 | | - it("allow dotted files and folders when explicitly requested", () => { |
143 | | - assertParseFileList( |
144 | | - `{ |
145 | | - "files": ["/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"] |
146 | | - }`, |
147 | | - "tsconfig.json", |
148 | | - "/apath", |
149 | | - ["/apath/test.ts", "/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"], |
150 | | - ["/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"] |
151 | | - ); |
152 | | - }); |
153 | | - |
154 | | - it("always exclude outDir", () => { |
155 | | - const tsconfigWithoutExclude = |
156 | | - `{ |
157 | | - "compilerOptions": { |
158 | | - "outDir": "bin" |
159 | | - } |
160 | | - }`; |
161 | | - const tsconfigWithExclude = |
162 | | - `{ |
163 | | - "compilerOptions": { |
164 | | - "outDir": "bin" |
165 | | - }, |
166 | | - "exclude": [ "obj" ] |
167 | | - }`; |
168 | | - const rootDir = "/"; |
169 | | - const allFiles = ["/bin/a.ts", "/b.ts"]; |
170 | | - const expectedFiles = ["/b.ts"]; |
171 | | - assertParseFileList(tsconfigWithoutExclude, "tsconfig.json", rootDir, allFiles, expectedFiles); |
172 | | - assertParseFileList(tsconfigWithExclude, "tsconfig.json", rootDir, allFiles, expectedFiles); |
173 | | - }); |
174 | | - |
175 | | - it("implicitly exclude common package folders", () => { |
176 | | - assertParseFileList( |
177 | | - `{}`, |
178 | | - "tsconfig.json", |
179 | | - "/", |
180 | | - ["/node_modules/a.ts", "/bower_components/b.ts", "/jspm_packages/c.ts", "/d.ts", "/folder/e.ts"], |
181 | | - ["/d.ts", "/folder/e.ts"] |
182 | | - ); |
183 | | - }); |
184 | | - }); |
185 | | -} |
| 1 | +/// <reference path="..\harness.ts" /> |
| 2 | +/// <reference path="..\..\compiler\commandLineParser.ts" /> |
| 3 | + |
| 4 | +namespace ts { |
| 5 | + describe("parseConfigFileTextToJson", () => { |
| 6 | + function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) { |
| 7 | + const parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); |
| 8 | + assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject)); |
| 9 | + } |
| 10 | + |
| 11 | + function assertParseError(jsonText: string) { |
| 12 | + const parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); |
| 13 | + assert.isTrue(undefined === parsed.config); |
| 14 | + assert.isTrue(undefined !== parsed.error); |
| 15 | + } |
| 16 | + |
| 17 | + function assertParseErrorWithExcludesKeyword(jsonText: string) { |
| 18 | + const parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); |
| 19 | + const parsedCommand = ts.parseJsonConfigFileContent(parsed.config, ts.sys, "tests/cases/unittests"); |
| 20 | + assert.isTrue(parsedCommand.errors && parsedCommand.errors.length === 1 && |
| 21 | + parsedCommand.errors[0].code === ts.Diagnostics.Unknown_option_excludes_Did_you_mean_exclude.code); |
| 22 | + } |
| 23 | + |
| 24 | + function assertParseFileList(jsonText: string, configFileName: string, basePath: string, allFileList: string[], expectedFileList: string[]) { |
| 25 | + const json = JSON.parse(jsonText); |
| 26 | + const host: ParseConfigHost = new Utils.MockParseConfigHost(basePath, true, allFileList); |
| 27 | + const parsed = ts.parseJsonConfigFileContent(json, host, basePath, /*existingOptions*/ undefined, configFileName); |
| 28 | + assert.isTrue(arrayIsEqualTo(parsed.fileNames.sort(), expectedFileList.sort())); |
| 29 | + } |
| 30 | + |
| 31 | + it("returns empty config for file with only whitespaces", () => { |
| 32 | + assertParseResult("", { config : {} }); |
| 33 | + assertParseResult(" ", { config : {} }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns empty config for file with comments only", () => { |
| 37 | + assertParseResult("// Comment", { config: {} }); |
| 38 | + assertParseResult("/* Comment*/", { config: {} }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns empty config when config is empty object", () => { |
| 42 | + assertParseResult("{}", { config: {} }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns config object without comments", () => { |
| 46 | + assertParseResult( |
| 47 | + `{ // Excluded files |
| 48 | + "exclude": [ |
| 49 | + // Exclude d.ts |
| 50 | + "file.d.ts" |
| 51 | + ] |
| 52 | + }`, { config: { exclude: ["file.d.ts"] } }); |
| 53 | + |
| 54 | + assertParseResult( |
| 55 | + `{ |
| 56 | + /* Excluded |
| 57 | + Files |
| 58 | + */ |
| 59 | + "exclude": [ |
| 60 | + /* multiline comments can be in the middle of a line */"file.d.ts" |
| 61 | + ] |
| 62 | + }`, { config: { exclude: ["file.d.ts"] } }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("keeps string content untouched", () => { |
| 66 | + assertParseResult( |
| 67 | + `{ |
| 68 | + "exclude": [ |
| 69 | + "xx//file.d.ts" |
| 70 | + ] |
| 71 | + }`, { config: { exclude: ["xx//file.d.ts"] } }); |
| 72 | + assertParseResult( |
| 73 | + `{ |
| 74 | + "exclude": [ |
| 75 | + "xx/*file.d.ts*/" |
| 76 | + ] |
| 77 | + }`, { config: { exclude: ["xx/*file.d.ts*/"] } }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("handles escaped characters in strings correctly", () => { |
| 81 | + assertParseResult( |
| 82 | + `{ |
| 83 | + "exclude": [ |
| 84 | + "xx\\"//files" |
| 85 | + ] |
| 86 | + }`, { config: { exclude: ["xx\"//files"] } }); |
| 87 | + |
| 88 | + assertParseResult( |
| 89 | + `{ |
| 90 | + "exclude": [ |
| 91 | + "xx\\\\" // end of line comment |
| 92 | + ] |
| 93 | + }`, { config: { exclude: ["xx\\"] } }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("returns object with error when json is invalid", () => { |
| 97 | + assertParseError("invalid"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("returns object when users correctly specify library", () => { |
| 101 | + assertParseResult( |
| 102 | + `{ |
| 103 | + "compilerOptions": { |
| 104 | + "lib": ["es5"] |
| 105 | + } |
| 106 | + }`, { |
| 107 | + config: { compilerOptions: { lib: ["es5"] } } |
| 108 | + }); |
| 109 | + |
| 110 | + assertParseResult( |
| 111 | + `{ |
| 112 | + "compilerOptions": { |
| 113 | + "lib": ["es5", "es6"] |
| 114 | + } |
| 115 | + }`, { |
| 116 | + config: { compilerOptions: { lib: ["es5", "es6"] } } |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns error when tsconfig have excludes", () => { |
| 121 | + assertParseErrorWithExcludesKeyword( |
| 122 | + `{ |
| 123 | + "compilerOptions": { |
| 124 | + "lib": ["es5"] |
| 125 | + }, |
| 126 | + "excludes": [ |
| 127 | + "foge.ts" |
| 128 | + ] |
| 129 | + }`); |
| 130 | + }); |
| 131 | + |
| 132 | + it("ignore dotted files and folders", () => { |
| 133 | + assertParseFileList( |
| 134 | + `{}`, |
| 135 | + "tsconfig.json", |
| 136 | + "/apath", |
| 137 | + ["/apath/test.ts", "/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"], |
| 138 | + ["/apath/test.ts"] |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it("allow dotted files and folders when explicitly requested", () => { |
| 143 | + assertParseFileList( |
| 144 | + `{ |
| 145 | + "files": ["/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"] |
| 146 | + }`, |
| 147 | + "tsconfig.json", |
| 148 | + "/apath", |
| 149 | + ["/apath/test.ts", "/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"], |
| 150 | + ["/apath/.git/a.ts", "/apath/.b.ts", "/apath/..c.ts"] |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it("always exclude outDir", () => { |
| 155 | + const tsconfigWithoutExclude = |
| 156 | + `{ |
| 157 | + "compilerOptions": { |
| 158 | + "outDir": "bin" |
| 159 | + } |
| 160 | + }`; |
| 161 | + const tsconfigWithExclude = |
| 162 | + `{ |
| 163 | + "compilerOptions": { |
| 164 | + "outDir": "bin" |
| 165 | + }, |
| 166 | + "exclude": [ "obj" ] |
| 167 | + }`; |
| 168 | + const rootDir = "/"; |
| 169 | + const allFiles = ["/bin/a.ts", "/b.ts"]; |
| 170 | + const expectedFiles = ["/b.ts"]; |
| 171 | + assertParseFileList(tsconfigWithoutExclude, "tsconfig.json", rootDir, allFiles, expectedFiles); |
| 172 | + assertParseFileList(tsconfigWithExclude, "tsconfig.json", rootDir, allFiles, expectedFiles); |
| 173 | + }); |
| 174 | + |
| 175 | + it("implicitly exclude common package folders", () => { |
| 176 | + assertParseFileList( |
| 177 | + `{}`, |
| 178 | + "tsconfig.json", |
| 179 | + "/", |
| 180 | + ["/node_modules/a.ts", "/bower_components/b.ts", "/jspm_packages/c.ts", "/d.ts", "/folder/e.ts"], |
| 181 | + ["/d.ts", "/folder/e.ts"] |
| 182 | + ); |
| 183 | + }); |
| 184 | + }); |
| 185 | +} |
0 commit comments