Skip to content

Commit a57dc59

Browse files
committed
More tests and updates
1 parent 9a506f9 commit a57dc59

File tree

49 files changed

+5709
-744
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+5709
-744
lines changed

internal/execute/testsys_test.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ import (
1515
"github.com/microsoft/typescript-go/internal/execute"
1616
"github.com/microsoft/typescript-go/internal/incremental"
1717
"github.com/microsoft/typescript-go/internal/testutil/incrementaltestutil"
18+
"github.com/microsoft/typescript-go/internal/testutil/stringtestutil"
1819
"github.com/microsoft/typescript-go/internal/tsoptions"
1920
"github.com/microsoft/typescript-go/internal/vfs"
2021
"github.com/microsoft/typescript-go/internal/vfs/vfstest"
2122
)
2223

2324
type FileMap map[string]any
2425

25-
var (
26-
tscLibPath = "/home/src/tslibs/TS/Lib"
27-
tscDefaultLibContent = `/// <reference no-default-lib="true"/>
26+
var tscLibPath = "/home/src/tslibs/TS/Lib"
27+
28+
var tscDefaultLibContent = stringtestutil.Dedent(`
29+
/// <reference no-default-lib="true"/>
2830
interface Boolean {}
2931
interface Function {}
3032
interface CallableFunction {}
@@ -45,8 +47,8 @@ declare var Symbol: SymbolConstructor;
4547
interface Symbol {
4648
readonly [Symbol.toStringTag]: string;
4749
}
48-
declare const console: { log(msg: any): void; };`
49-
)
50+
declare const console: { log(msg: any): void; };
51+
`)
5052

5153
func newTestSys(fileOrFolderList FileMap, cwd string) *testSys {
5254
if cwd == "" {
@@ -256,12 +258,10 @@ func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
256258
}
257259
if s.serializedDiff != nil {
258260
for path := range s.serializedDiff.snap {
259-
if s.FS().FileExists(path) {
260-
_, ok := s.TestFS().FS().ReadFile(path)
261-
if !ok {
262-
// report deleted
263-
s.reportFSEntryDiff(baseline, nil, path)
264-
}
261+
_, ok := s.TestFS().FS().ReadFile(path)
262+
if !ok {
263+
// report deleted
264+
s.reportFSEntryDiff(baseline, nil, path)
265265
}
266266
}
267267
}
@@ -322,3 +322,19 @@ func (s *testSys) ReplaceFileText(path string, oldText string, newText string) {
322322
content = strings.Replace(content, oldText, newText, 1)
323323
s.WriteFileNoError(path, content, false)
324324
}
325+
326+
func (s *testSys) AppendFile(path string, text string) {
327+
content, ok := s.FS().ReadFile(path)
328+
if !ok {
329+
panic("File not found: " + path)
330+
}
331+
s.WriteFileNoError(path, content+text, false)
332+
}
333+
334+
func (s *testSys) PrependFile(path string, text string) {
335+
content, ok := s.FS().ReadFile(path)
336+
if !ok {
337+
panic("File not found: " + path)
338+
}
339+
s.WriteFileNoError(path, text+content, false)
340+
}

internal/execute/tsc_test.go

Lines changed: 118 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package execute_test
22

33
import (
44
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/testutil/stringtestutil"
57
)
68

79
func TestTscCommandline(t *testing.T) {
@@ -60,32 +62,56 @@ func TestTscCommandline(t *testing.T) {
6062
{
6163
subScenario: "Project is empty string",
6264
sys: newTestSys(FileMap{
63-
"/home/src/workspaces/project/first.ts": `export const a = 1`,
64-
"/home/src/workspaces/project/tsconfig.json": `{ "compilerOptions": { "strict": true, "noEmit": true } }`,
65+
"/home/src/workspaces/project/first.ts": `export const a = 1`,
66+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
67+
{
68+
"compilerOptions": {
69+
"strict": true,
70+
"noEmit": true
71+
}
72+
}`),
6573
}, ""),
6674
commandLineArgs: []string{},
6775
},
6876
{
6977
subScenario: "Parse -p",
7078
sys: newTestSys(FileMap{
71-
"/home/src/workspaces/project/first.ts": `export const a = 1`,
72-
"/home/src/workspaces/project/tsconfig.json": `{ "compilerOptions": { "strict": true, "noEmit": true } }`,
79+
"/home/src/workspaces/project/first.ts": `export const a = 1`,
80+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
81+
{
82+
"compilerOptions": {
83+
"strict": true,
84+
"noEmit": true
85+
}
86+
}`),
7387
}, ""),
7488
commandLineArgs: []string{"-p", "."},
7589
},
7690
{
7791
subScenario: "Parse -p with path to tsconfig file",
7892
sys: newTestSys(FileMap{
79-
"/home/src/workspaces/project/first.ts": `export const a = 1`,
80-
"/home/src/workspaces/project/tsconfig.json": `{ "compilerOptions": { "strict": true, "noEmit": true } }`,
93+
"/home/src/workspaces/project/first.ts": `export const a = 1`,
94+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
95+
{
96+
"compilerOptions": {
97+
"strict": true,
98+
"noEmit": true
99+
}
100+
}`),
81101
}, ""),
82102
commandLineArgs: []string{"-p", "/home/src/workspaces/project/tsconfig.json"},
83103
},
84104
{
85105
subScenario: "Parse -p with path to tsconfig folder",
86106
sys: newTestSys(FileMap{
87-
"/home/src/workspaces/project/first.ts": `export const a = 1`,
88-
"/home/src/workspaces/project/tsconfig.json": `{ "compilerOptions": { "strict": true, "noEmit": true } }`,
107+
"/home/src/workspaces/project/first.ts": `export const a = 1`,
108+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
109+
{
110+
"compilerOptions": {
111+
"strict": true,
112+
"noEmit": true
113+
}
114+
}`),
89115
}, ""),
90116
commandLineArgs: []string{"-p", "/home/src/workspaces/project"},
91117
},
@@ -97,8 +123,14 @@ func TestTscCommandline(t *testing.T) {
97123
{
98124
subScenario: "Parse watch interval option",
99125
sys: newTestSys(FileMap{
100-
"/home/src/workspaces/project/first.ts": `export const a = 1`,
101-
"/home/src/workspaces/project/tsconfig.json": `{ "compilerOptions": { "strict": true, "noEmit": true } }`,
126+
"/home/src/workspaces/project/first.ts": `export const a = 1`,
127+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
128+
{
129+
"compilerOptions": {
130+
"strict": true,
131+
"noEmit": true
132+
}
133+
}`),
102134
}, ""),
103135
commandLineArgs: []string{"-w", "--watchInterval", "1000"},
104136
},
@@ -119,12 +151,13 @@ func TestNoEmit(t *testing.T) {
119151
(&tscInput{
120152
subScenario: "when project has strict true",
121153
sys: newTestSys(FileMap{
122-
"/home/src/workspaces/project/tsconfig.json": `{
123-
"compilerOptions": {
124-
"incremental": true,
125-
"strict": true,
126-
},
127-
}`,
154+
"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
155+
{
156+
"compilerOptions": {
157+
"incremental": true,
158+
"strict": true
159+
}
160+
}`),
128161
"/home/src/workspaces/project/class1.ts": `export class class1 {}`,
129162
}, ""),
130163
commandLineArgs: []string{"--noEmit"},
@@ -133,67 +166,72 @@ func TestNoEmit(t *testing.T) {
133166

134167
func TestExtends(t *testing.T) {
135168
t.Parallel()
136-
extendsSysFiles := FileMap{
137-
"/home/src/projects/configs/first/tsconfig.json": `{
138-
"extends": "../second/tsconfig.json",
139-
"include": ["${configDir}/src"],
140-
"compilerOptions": {
141-
"typeRoots": ["root1", "${configDir}/root2", "root3"],
142-
"types": [],
143-
},
144-
}`,
145-
"/home/src/projects/configs/second/tsconfig.json": `{
146-
"files": ["${configDir}/main.ts"],
147-
"compilerOptions": {
148-
"declarationDir": "${configDir}/decls",
149-
"paths": {
150-
"@myscope/*": ["${configDir}/types/*"],
151-
"other/*": ["other/*"],
152-
},
153-
"baseUrl": "${configDir}",
154-
},
155-
"watchOptions": {
156-
"excludeFiles": ["${configDir}/main.ts"],
157-
},
158-
}`,
159-
"/home/src/projects/myproject/tsconfig.json": `{
160-
"extends": "../configs/first/tsconfig.json",
161-
"compilerOptions": {
162-
"declaration": true,
163-
"outDir": "outDir",
164-
"traceResolution": true,
165-
},
166-
}`,
167-
168-
"/home/src/projects/myproject/main.ts": `
169-
// some comment
170-
export const y = 10;
171-
import { x } from "@myscope/sometype";
172-
`,
173-
"/home/src/projects/myproject/src/secondary.ts": `
174-
// some comment
175-
export const z = 10;
176-
import { k } from "other/sometype2";
177-
`,
178-
"/home/src/projects/myproject/types/sometype.ts": `
179-
export const x = 10;
180-
`,
181-
"/home/src/projects/myproject/root2/other/sometype2/index.d.ts": `
182-
export const k = 10;
183-
`,
169+
extendsSys := func() *testSys {
170+
return newTestSys(FileMap{
171+
"/home/src/projects/configs/first/tsconfig.json": stringtestutil.Dedent(`
172+
{
173+
"extends": "../second/tsconfig.json",
174+
"include": ["${configDir}/src"],
175+
"compilerOptions": {
176+
"typeRoots": ["root1", "${configDir}/root2", "root3"],
177+
"types": [],
178+
}
179+
}`),
180+
"/home/src/projects/configs/second/tsconfig.json": stringtestutil.Dedent(`
181+
{
182+
"files": ["${configDir}/main.ts"],
183+
"compilerOptions": {
184+
"declarationDir": "${configDir}/decls",
185+
"paths": {
186+
"@myscope/*": ["${configDir}/types/*"],
187+
"other/*": ["other/*"],
188+
},
189+
"baseUrl": "${configDir}",
190+
},
191+
"watchOptions": {
192+
"excludeFiles": ["${configDir}/main.ts"],
193+
},
194+
}`),
195+
"/home/src/projects/myproject/tsconfig.json": stringtestutil.Dedent(`
196+
{
197+
"extends": "../configs/first/tsconfig.json",
198+
"compilerOptions": {
199+
"declaration": true,
200+
"outDir": "outDir",
201+
"traceResolution": true,
202+
},
203+
}`),
204+
"/home/src/projects/myproject/main.ts": stringtestutil.Dedent(`
205+
// some comment
206+
export const y = 10;
207+
import { x } from "@myscope/sometype";
208+
`),
209+
"/home/src/projects/myproject/src/secondary.ts": stringtestutil.Dedent(`
210+
// some comment
211+
export const z = 10;
212+
import { k } from "other/sometype2";
213+
`),
214+
"/home/src/projects/myproject/types/sometype.ts": stringtestutil.Dedent(`
215+
// some comment
216+
export const x = 10;
217+
`),
218+
"/home/src/projects/myproject/root2/other/sometype2/index.d.ts": stringtestutil.Dedent(`
219+
export const k = 10;
220+
`),
221+
}, "/home/src/projects/myproject")
184222
}
185223

186224
cases := []tscInput{{
187225
subScenario: "configDir template",
188-
sys: newTestSys(extendsSysFiles, "/home/src/projects/myproject"),
226+
sys: extendsSys(),
189227
commandLineArgs: []string{"--explainFiles"},
190228
}, {
191229
subScenario: "configDir template showConfig",
192-
sys: newTestSys(extendsSysFiles, "/home/src/projects/myproject"),
230+
sys: extendsSys(),
193231
commandLineArgs: []string{"--showConfig"},
194232
}, {
195233
subScenario: "configDir template with commandline",
196-
sys: newTestSys(extendsSysFiles, "/home/src/projects/myproject"),
234+
sys: extendsSys(),
197235
commandLineArgs: []string{"--explainFiles", "--outDir", "${configDir}/outDir"},
198236
}}
199237

@@ -206,20 +244,19 @@ func TestTypeAcquisition(t *testing.T) {
206244
t.Parallel()
207245
(&tscInput{
208246
subScenario: "parse tsconfig with typeAcquisition",
209-
sys: newTestSys(FileMap{"/home/src/workspaces/project/tsconfig.json": `{
210-
"compilerOptions": {
211-
"composite": true,
212-
"noEmit": true,
213-
},
214-
"typeAcquisition": {
215-
"enable": true,
216-
"include": ["0.d.ts", "1.d.ts"],
217-
"exclude": ["0.js", "1.js"],
218-
"disableFilenameBasedTypeAcquisition": true,
219-
},
220-
}`},
221-
"/home/src/workspaces/project",
222-
),
247+
sys: newTestSys(FileMap{"/home/src/workspaces/project/tsconfig.json": stringtestutil.Dedent(`
248+
{
249+
"compilerOptions": {
250+
"composite": true,
251+
"noEmit": true,
252+
},
253+
"typeAcquisition": {
254+
"enable": true,
255+
"include": ["0.d.ts", "1.d.ts"],
256+
"exclude": ["0.js", "1.js"],
257+
"disableFilenameBasedTypeAcquisition": true,
258+
},
259+
}`)}, "/home/src/workspaces/project"),
223260
commandLineArgs: []string{},
224261
}).run(t, "typeAcquisition")
225262
}

0 commit comments

Comments
 (0)