Skip to content

Commit d8ca80e

Browse files
committed
sanitize tracing in tsc tests
1 parent 4a8c488 commit d8ca80e

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

internal/execute/testsys_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func newTestSys(tscInput *tscInput) *testSys {
6363
if tscInput.windowsStyleRoot != "" {
6464
libPath = tscInput.windowsStyleRoot + libPath[1:]
6565
}
66+
currentWrite := &strings.Builder{}
6667
sys := &testSys{
6768
fs: &incrementaltestutil.FsHandlingBuildInfo{
6869
FS: &testFs{
@@ -71,9 +72,13 @@ func newTestSys(tscInput *tscInput) *testSys {
7172
},
7273
defaultLibraryPath: libPath,
7374
cwd: cwd,
74-
currentWrite: &strings.Builder{},
75-
start: time.Now(),
76-
env: tscInput.env,
75+
currentWrite: currentWrite,
76+
tracer: harnessutil.NewTracerForBaselining(tspath.ComparePathsOptions{
77+
UseCaseSensitiveFileNames: !tscInput.ignoreCase,
78+
CurrentDirectory: cwd,
79+
}, currentWrite),
80+
start: time.Now(),
81+
env: tscInput.env,
7782
}
7883

7984
// Ensure the default library file is present
@@ -101,6 +106,7 @@ type snapshot struct {
101106

102107
type testSys struct {
103108
currentWrite *strings.Builder
109+
tracer *harnessutil.TracerForBaselining
104110
serializedDiff *snapshot
105111

106112
fs *incrementaltestutil.FsHandlingBuildInfo
@@ -194,6 +200,14 @@ func (s *testSys) OnStatisticsEnd() {
194200
fmt.Fprintln(s.Writer(), statisticsEnd)
195201
}
196202

203+
func (s *testSys) GetTrace() func(str string) {
204+
return func(str string) {
205+
fmt.Fprintln(s.currentWrite, traceStart)
206+
defer fmt.Fprintln(s.currentWrite, traceEnd)
207+
s.tracer.Trace(str)
208+
}
209+
}
210+
197211
func (s *testSys) baselineProgram(baseline *strings.Builder, program *incremental.Program, watcher *execute.Watcher) {
198212
if watcher != nil {
199213
program = watcher.GetProgram()
@@ -257,6 +271,8 @@ var (
257271
listFileEnd = "!!! List files end"
258272
statisticsStart = "!!! Statistics start"
259273
statisticsEnd = "!!! Statistics end"
274+
traceStart = "!!! Trace start"
275+
traceEnd = "!!! Trace end"
260276
)
261277

262278
func (s *testSys) baselineOutput(baseline io.Writer) {
@@ -296,7 +312,8 @@ func (o *outputSanitizer) transformLines() string {
296312
continue
297313
}
298314
if !o.addOrSkipLinesForComparing(listFileStart, listFileEnd, false) &&
299-
!o.addOrSkipLinesForComparing(statisticsStart, statisticsEnd, true) {
315+
!o.addOrSkipLinesForComparing(statisticsStart, statisticsEnd, true) &&
316+
!o.addOrSkipLinesForComparing(traceStart, traceEnd, false) {
300317
o.addOutputLine(line)
301318
}
302319
}
@@ -335,6 +352,7 @@ func (s *testSys) getOutput(forComparing bool) string {
335352

336353
func (s *testSys) clearOutput() {
337354
s.currentWrite.Reset()
355+
s.tracer.Reset()
338356
}
339357

340358
func (s *testSys) baselineFSwithDiff(baseline io.Writer) {

internal/execute/tsc.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type CommandLineTesting interface {
5050
OnListFilesEnd()
5151
OnStatisticsStart()
5252
OnStatisticsEnd()
53+
GetTrace() func(str string)
5354
}
5455

5556
func CommandLine(sys System, commandLineArgs []string, testing CommandLineTesting) CommandLineResult {
@@ -228,9 +229,13 @@ func findConfigFile(searchPath string, fileExists func(string) bool, configName
228229
return result
229230
}
230231

231-
func getTraceFromSys(sys System) func(msg string) {
232-
return func(msg string) {
233-
fmt.Fprintln(sys.Writer(), msg)
232+
func getTraceFromSys(sys System, testing CommandLineTesting) func(msg string) {
233+
if testing == nil {
234+
return func(msg string) {
235+
fmt.Fprintln(sys.Writer(), msg)
236+
}
237+
} else {
238+
return testing.GetTrace()
234239
}
235240
}
236241

@@ -242,7 +247,7 @@ func performIncrementalCompilation(
242247
configTime time.Duration,
243248
testing CommandLineTesting,
244249
) CommandLineResult {
245-
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys))
250+
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys, testing))
246251
buildInfoReadStart := sys.Now()
247252
oldProgram := incremental.ReadBuildInfoProgram(config, incremental.NewBuildInfoReader(host), host)
248253
buildInfoReadTime := sys.Now().Sub(buildInfoReadStart)
@@ -283,7 +288,7 @@ func performCompilation(
283288
configTime time.Duration,
284289
testing CommandLineTesting,
285290
) CommandLineResult {
286-
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys))
291+
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys, testing))
287292
// todo: cache, statistics, tracing
288293
parseStart := sys.Now()
289294
program := compiler.NewProgram(compiler.ProgramOptions{

internal/execute/watcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func createWatcher(sys System, configParseResult *tsoptions.ParsedCommandLine, r
4242
}
4343

4444
func (w *Watcher) start() {
45-
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), nil, getTraceFromSys(w.sys))
45+
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), nil, getTraceFromSys(w.sys, w.testing))
4646
w.program = incremental.ReadBuildInfoProgram(w.options, incremental.NewBuildInfoReader(w.host), w.host)
4747

4848
if w.testing == nil {
@@ -93,8 +93,8 @@ func (w *Watcher) compileAndEmit() {
9393

9494
func (w *Watcher) hasErrorsInTsConfig() bool {
9595
// only need to check and reparse tsconfig options/update host if we are watching a config file
96+
extendedConfigCache := collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]{}
9697
if w.configFileName != "" {
97-
extendedConfigCache := collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]{}
9898
// !!! need to check that this merges compileroptions correctly. This differs from non-watch, since we allow overriding of previous options
9999
configParseResult, errors := tsoptions.GetParsedCommandLineOfConfigFile(w.configFileName, &core.CompilerOptions{}, w.sys, &extendedConfigCache)
100100
if len(errors) > 0 {
@@ -109,8 +109,8 @@ func (w *Watcher) hasErrorsInTsConfig() bool {
109109
w.configModified = true
110110
}
111111
w.options = configParseResult
112-
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), &extendedConfigCache, getTraceFromSys(w.sys))
113112
}
113+
w.host = compiler.NewCompilerHost(w.sys.GetCurrentDirectory(), w.sys.FS(), w.sys.DefaultLibraryPath(), &extendedConfigCache, getTraceFromSys(w.sys, w.testing))
114114
return false
115115
}
116116

internal/testutil/harnessutil/harnessutil.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func CompileFilesEx(
223223
Errors: errors,
224224
}, harnessOptions)
225225
result.Symlinks = symlinks
226-
result.Trace = host.tracer.string()
226+
result.Trace = host.tracer.String()
227227
result.Repeat = func(testConfig TestConfiguration) *CompilationResult {
228228
newHarnessOptions := *harnessOptions
229229
newCompilerOptions := compilerOptions.Clone()
@@ -474,7 +474,7 @@ func getOptionValue(t *testing.T, option *tsoptions.CommandLineOption, value str
474474

475475
type cachedCompilerHost struct {
476476
compiler.CompilerHost
477-
tracer *tracer
477+
tracer *TracerForBaselining
478478
}
479479

480480
var sourceFileCache collections.SyncMap[SourceFileCacheKey, *ast.SourceFile]
@@ -515,26 +515,33 @@ func (h *cachedCompilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast
515515
return result
516516
}
517517

518-
type tracer struct {
519-
fs vfs.FS
520-
currentDirectory string
518+
type TracerForBaselining struct {
519+
opts tspath.ComparePathsOptions
521520
packageJsonCache map[tspath.Path]bool
522-
builder strings.Builder
521+
builder *strings.Builder
523522
}
524523

525-
func (t *tracer) trace(msg string) {
526-
fmt.Fprintln(&t.builder, t.sanitizeTrace(msg))
524+
func NewTracerForBaselining(opts tspath.ComparePathsOptions, builder *strings.Builder) *TracerForBaselining {
525+
return &TracerForBaselining{
526+
opts: opts,
527+
packageJsonCache: make(map[tspath.Path]bool),
528+
builder: builder,
529+
}
530+
}
531+
532+
func (t *TracerForBaselining) Trace(msg string) {
533+
fmt.Fprintln(t.builder, t.sanitizeTrace(msg))
527534
}
528535

529-
func (t *tracer) sanitizeTrace(msg string) string {
536+
func (t *TracerForBaselining) sanitizeTrace(msg string) string {
530537
// Version
531538
if str := strings.Replace(msg, "'"+core.Version()+"'", "'"+FakeTSVersion+"'", 1); str != msg {
532539
return str
533540
}
534541
// caching of fs in trace to be replaces with non caching version
535542
if str := strings.TrimSuffix(msg, "' does not exist according to earlier cached lookups."); str != msg {
536543
file := strings.TrimPrefix(str, "File '")
537-
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
544+
filePath := tspath.ToPath(file, t.opts.CurrentDirectory, t.opts.UseCaseSensitiveFileNames)
538545
if _, has := t.packageJsonCache[filePath]; has {
539546
return msg
540547
} else {
@@ -544,7 +551,7 @@ func (t *tracer) sanitizeTrace(msg string) string {
544551
}
545552
if str := strings.TrimSuffix(msg, "' does not exist."); str != msg {
546553
file := strings.TrimPrefix(str, "File '")
547-
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
554+
filePath := tspath.ToPath(file, t.opts.CurrentDirectory, t.opts.UseCaseSensitiveFileNames)
548555
if _, has := t.packageJsonCache[filePath]; !has {
549556
t.packageJsonCache[filePath] = false
550557
return msg
@@ -554,7 +561,7 @@ func (t *tracer) sanitizeTrace(msg string) string {
554561
}
555562
if str := strings.TrimSuffix(msg, "' exists according to earlier cached lookups."); str != msg {
556563
file := strings.TrimPrefix(str, "File '")
557-
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
564+
filePath := tspath.ToPath(file, t.opts.CurrentDirectory, t.opts.UseCaseSensitiveFileNames)
558565
if _, has := t.packageJsonCache[filePath]; has {
559566
return msg
560567
} else {
@@ -564,7 +571,7 @@ func (t *tracer) sanitizeTrace(msg string) string {
564571
}
565572
if str := strings.TrimPrefix(msg, "Found 'package.json' at '"); str != msg {
566573
file := strings.TrimSuffix(str, "'.")
567-
filePath := tspath.ToPath(file, t.currentDirectory, t.fs.UseCaseSensitiveFileNames())
574+
filePath := tspath.ToPath(file, t.opts.CurrentDirectory, t.opts.UseCaseSensitiveFileNames)
568575
if _, has := t.packageJsonCache[filePath]; !has {
569576
t.packageJsonCache[filePath] = true
570577
return msg
@@ -575,15 +582,22 @@ func (t *tracer) sanitizeTrace(msg string) string {
575582
return msg
576583
}
577584

578-
func (t *tracer) string() string {
585+
func (t *TracerForBaselining) String() string {
579586
return t.builder.String()
580587
}
581588

589+
func (t *TracerForBaselining) Reset() {
590+
t.packageJsonCache = make(map[tspath.Path]bool)
591+
}
592+
582593
func createCompilerHost(fs vfs.FS, defaultLibraryPath string, currentDirectory string) *cachedCompilerHost {
583-
tracer := tracer{fs: fs, currentDirectory: currentDirectory, packageJsonCache: make(map[tspath.Path]bool)}
594+
tracer := NewTracerForBaselining(tspath.ComparePathsOptions{
595+
UseCaseSensitiveFileNames: fs.UseCaseSensitiveFileNames(),
596+
CurrentDirectory: currentDirectory,
597+
}, &strings.Builder{})
584598
return &cachedCompilerHost{
585-
CompilerHost: compiler.NewCompilerHost(currentDirectory, fs, defaultLibraryPath, nil, tracer.trace),
586-
tracer: &tracer,
599+
CompilerHost: compiler.NewCompilerHost(currentDirectory, fs, defaultLibraryPath, nil, tracer.Trace),
600+
tracer: tracer,
587601
}
588602
}
589603

0 commit comments

Comments
 (0)