Skip to content

Commit f4a79e2

Browse files
committed
Remove EndWrite and use string builder directly for baselining
1 parent 9933123 commit f4a79e2

31 files changed

+45
-87
lines changed

cmd/tsgo/sys.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ func (s *osSys) Writer() io.Writer {
4646
return s.writer
4747
}
4848

49-
func (s *osSys) EndWrite() {
50-
// do nothing, this is needed in the interface for testing
51-
// todo: revisit if improving tsc/build/watch unittest baselines
52-
}
53-
5449
func (s *osSys) WriteOutputIsTTY() bool {
5550
return term.IsTerminal(int(os.Stdout.Fd()))
5651
}

internal/execute/outputs.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ func createDiagnosticReporter(sys System, options *core.CompilerOptions) diagnos
4040
if !shouldBePretty(sys, options) {
4141
return func(diagnostic *ast.Diagnostic) {
4242
diagnosticwriter.WriteFormatDiagnostic(sys.Writer(), diagnostic, formatOpts)
43-
sys.EndWrite()
4443
}
4544
}
4645
return func(diagnostic *ast.Diagnostic) {
4746
diagnosticwriter.FormatDiagnosticsWithColorAndContext(sys.Writer(), []*ast.Diagnostic{diagnostic}, formatOpts)
48-
sys.EndWrite()
4947
}
5048
}
5149

@@ -132,7 +130,6 @@ func createReportErrorSummary(sys System, options *core.CompilerOptions) func(di
132130
formatOpts := getFormatOptsOfSys(sys)
133131
return func(diagnostics []*ast.Diagnostic) {
134132
diagnosticwriter.WriteErrorSummaryText(sys.Writer(), diagnostics, formatOpts)
135-
sys.EndWrite()
136133
}
137134
}
138135
return func(diagnostics []*ast.Diagnostic) {}
@@ -175,7 +172,6 @@ func reportStatistics(sys System, program *compiler.Program, result compileAndEm
175172

176173
func printVersion(sys System) {
177174
fmt.Fprintln(sys.Writer(), diagnostics.Version_0.Format(core.Version()))
178-
sys.EndWrite()
179175
}
180176

181177
func printHelp(sys System, commandLine *tsoptions.ParsedCommandLine) {
@@ -268,7 +264,6 @@ func printEasyHelp(sys System, simpleOptions []*tsoptions.CommandLineOption) {
268264
for _, chunk := range output {
269265
fmt.Fprint(sys.Writer(), chunk)
270266
}
271-
sys.EndWrite()
272267
}
273268

274269
func generateSectionOptionsOutput(

internal/execute/system.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
type System interface {
1111
Writer() io.Writer
12-
EndWrite() // needed for testing
1312
FS() vfs.FS
1413
DefaultLibraryPath() string
1514
GetCurrentDirectory() string

internal/execute/testsys_test.go

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func newTestSys(tscInput *tscInput) *testSys {
7171
},
7272
defaultLibraryPath: libPath,
7373
cwd: cwd,
74-
output: []string{},
7574
currentWrite: &strings.Builder{},
7675
start: time.Now(),
7776
env: tscInput.env,
@@ -101,8 +100,6 @@ type snapshot struct {
101100
}
102101

103102
type testSys struct {
104-
// todo: original has write to output as a string[] because the separations are needed for baselining
105-
output []string
106103
currentWrite *strings.Builder
107104
serializedDiff *snapshot
108105

@@ -179,23 +176,16 @@ func (s *testSys) GetEnvironmentVariable(name string) string {
179176
}
180177

181178
func sanitizeSysOutput(output string, prefixLine string, replaceString string) string {
182-
if index := strings.Index(output, prefixLine); index != -1 {
183-
indexOfNewLine := strings.Index(output[index:], "\n")
184-
if indexOfNewLine != -1 {
185-
output = output[:index] + replaceString + output[index+indexOfNewLine+1:]
179+
for {
180+
if index := strings.Index(output, prefixLine); index != -1 {
181+
indexOfNewLine := strings.Index(output[index:], "\n")
182+
if indexOfNewLine != -1 {
183+
output = output[:index] + replaceString + output[index+indexOfNewLine+1:]
184+
continue
185+
}
186186
}
187+
return output
187188
}
188-
return output
189-
}
190-
191-
func (s *testSys) EndWrite() {
192-
// todo: revisit if improving tsc/build/watch unittest baselines
193-
output := s.currentWrite.String()
194-
s.currentWrite.Reset()
195-
output = sanitizeSysOutput(output, "Version "+core.Version(), "Version "+harnessutil.FakeTSVersion+"\n")
196-
output = sanitizeSysOutput(output, "build starting at ", "")
197-
output = sanitizeSysOutput(output, "build finished in ", "")
198-
s.output = append(s.output, output)
199189
}
200190

201191
func (s *testSys) baselineProgram(baseline *strings.Builder, program *incremental.Program, watcher *execute.Watcher) {
@@ -253,16 +243,19 @@ func (s *testSys) serializeState(baseline *strings.Builder) {
253243

254244
func (s *testSys) baselineOutput(baseline io.Writer) {
255245
fmt.Fprint(baseline, "\nOutput::\n")
256-
if len(s.output) == 0 {
257-
fmt.Fprint(baseline, "No output\n")
258-
return
259-
}
260-
// todo screen clears
261-
s.printOutputs(baseline)
246+
fmt.Fprint(baseline, s.getOutput())
247+
}
248+
249+
func (s *testSys) getOutput() string {
250+
output := s.currentWrite.String()
251+
output = sanitizeSysOutput(output, "Version "+core.Version(), "Version "+harnessutil.FakeTSVersion+"\n")
252+
output = sanitizeSysOutput(output, "build starting at ", "")
253+
output = sanitizeSysOutput(output, "build finished in ", "")
254+
return output
262255
}
263256

264257
func (s *testSys) clearOutput() {
265-
s.output = []string{}
258+
s.currentWrite.Reset()
266259
}
267260

268261
func (s *testSys) baselineFSwithDiff(baseline io.Writer) {
@@ -368,11 +361,6 @@ func (s *testSys) addFsEntryDiff(diffs map[string]string, newDirContent *diffEnt
368361
}
369362
}
370363

371-
func (s *testSys) printOutputs(baseline io.Writer) {
372-
// todo sanitize sys output
373-
fmt.Fprint(baseline, strings.Join(s.output, "\n"))
374-
}
375-
376364
func (s *testSys) writeFileNoError(path string, content string, writeByteOrderMark bool) {
377365
if err := s.fsFromFileMap().WriteFile(path, content, writeByteOrderMark); err != nil {
378366
panic(err)

internal/execute/tsc.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func CommandLine(sys System, commandLineArgs []string, testing bool) CommandLine
5353
switch strings.ToLower(commandLineArgs[0]) {
5454
case "-b", "--b", "-build", "--build":
5555
fmt.Fprintln(sys.Writer(), "Build mode is currently unsupported.")
56-
sys.EndWrite()
5756
return CommandLineResult{Status: ExitStatusNotImplemented}
5857
// case "-f":
5958
// return fmtMain(sys, commandLineArgs[1], commandLineArgs[1])

internal/execute/tsctestrunner_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func getDiffForIncremental(incrementalSys *testSys, nonIncrementalSys *testSys)
160160
}
161161
}
162162

163-
incrementalOutput := strings.Join(incrementalSys.output, "")
164-
nonIncrementalOutput := strings.Join(nonIncrementalSys.output, "")
163+
incrementalOutput := incrementalSys.getOutput()
164+
nonIncrementalOutput := nonIncrementalSys.getOutput()
165165
if incrementalOutput != nonIncrementalOutput {
166166
diffBuilder.WriteString(baseline.DiffText("nonIncremental.output.txt", "incremental.output.txt", nonIncrementalOutput, incrementalOutput))
167167
}

testdata/baselines/reference/tsbuild/commandLine/when-build-not-first-argument.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ Input::
55
tsgo --verbose --build
66
ExitStatus:: DiagnosticsPresent_OutputsSkipped
77
Output::
8-
error TS5093: Compiler option '--verbose' may only be used with '--build'.
9-
error TS6369: Option '--build' must be the first command line argument.
8+
error TS5093: Compiler option '--verbose' may only be used with '--build'.error TS6369: Option '--build' must be the first command line argument.

testdata/baselines/reference/tsc/commandLine/does-not-add-color-when-NO_COLOR-is-set.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tsgo
66
ExitStatus:: DiagnosticsPresent_OutputsSkipped
77
Output::
88
Version FakeTSVersion
9-
109
tsc: The TypeScript Compiler - Version FakeTSVersion
1110

1211
COMMON COMMANDS

testdata/baselines/reference/tsc/commandLine/help-all.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ Input::
55
tsgo --help --all
66
ExitStatus:: Success
77
Output::
8-
No output
98

testdata/baselines/reference/tsc/commandLine/show-help-with-ExitStatus.DiagnosticsPresent_OutputsSkipped-when-host-cannot-provide-terminal-width.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ tsgo
66
ExitStatus:: DiagnosticsPresent_OutputsSkipped
77
Output::
88
Version FakeTSVersion
9-
109
tsc: The TypeScript Compiler - Version FakeTSVersion
1110

1211
COMMON COMMANDS

0 commit comments

Comments
 (0)