Skip to content

Commit 4675e42

Browse files
committed
Test program stuff
1 parent ca9c055 commit 4675e42

File tree

7 files changed

+102
-85
lines changed

7 files changed

+102
-85
lines changed

Herebyfile.mjs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ function parseEnvBoolean(name, defaultValue = false) {
5151
throw new Error(`Invalid value for ${name}: ${value}`);
5252
}
5353

54+
/**
55+
* @param {string} name
56+
* @param {string} defaultValue
57+
* @returns {string}
58+
*/
59+
function parseEnvString(name, defaultValue = "") {
60+
name = "TSGO_HEREBY_" + name.toUpperCase();
61+
const value = process.env[name];
62+
if (!value) {
63+
return defaultValue;
64+
}
65+
return value;
66+
}
67+
5468
const { values: rawOptions } = parseArgs({
5569
args: process.argv.slice(2),
5670
options: {
@@ -65,7 +79,7 @@ const { values: rawOptions } = parseArgs({
6579

6680
race: { type: "boolean", default: parseEnvBoolean("RACE") },
6781
noembed: { type: "boolean", default: parseEnvBoolean("NOEMBED") },
68-
concurrentTestPrograms: { type: "boolean", default: parseEnvBoolean("CONCURRENT_TEST_PROGRAMS") },
82+
concurrency: { type: "string", default: parseEnvString("TEST_PROGRAM_CONCURRENCY") },
6983
coverage: { type: "boolean", default: parseEnvBoolean("COVERAGE") },
7084
},
7185
strict: false,
@@ -291,7 +305,7 @@ function goTestFlags(taskName) {
291305
}
292306

293307
const goTestEnv = {
294-
...(options.concurrentTestPrograms ? { TS_TEST_PROGRAM_SINGLE_THREADED: "false" } : {}),
308+
...(options.concurrency ? { TSGO_TEST_PROGRAM_CONCURRENCY: "false" } : {}),
295309
// Go test caching takes a long time on Windows.
296310
// https://github.com/golang/go/issues/72992
297311
...(process.platform === "win32" ? { GOFLAGS: "-count=1" } : {}),

internal/compiler/concurrency.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

internal/compiler/program.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Program struct {
3939
opts ProgramOptions
4040
nodeModules map[string]*ast.SourceFile
4141

42-
concurrency concurrency
42+
concurrency core.Concurrency
4343
concurrencyOnce sync.Once
4444
checkerPool CheckerPool
4545
checkerPoolOnce sync.Once
@@ -290,23 +290,23 @@ func (p *Program) GetConfigFileParsingDiagnostics() []*ast.Diagnostic {
290290
return slices.Clip(p.opts.Config.GetConfigFileParsingDiagnostics())
291291
}
292292

293-
func (p *Program) getConcurrency() concurrency {
293+
func (p *Program) getConcurrency() core.Concurrency {
294294
p.concurrencyOnce.Do(func() {
295-
p.concurrency = parseConcurrency(p.Options(), len(p.files))
295+
p.concurrency = core.ParseConcurrency(p.Options())
296296
})
297297
return p.concurrency
298298
}
299299

300300
func (p *Program) singleThreaded() bool {
301-
return p.getConcurrency().isSingleThreaded()
301+
return p.getConcurrency().SingleThreaded()
302302
}
303303

304304
func (p *Program) getCheckerPool() CheckerPool {
305305
p.checkerPoolOnce.Do(func() {
306306
if p.opts.CreateCheckerPool != nil {
307307
p.checkerPool = p.opts.CreateCheckerPool(p)
308308
} else {
309-
p.checkerPool = newCheckerPool(p.getConcurrency().getCheckerCount(len(p.files)), p)
309+
p.checkerPool = newCheckerPool(p.getConcurrency().CheckerCount(len(p.files)), p)
310310
}
311311
})
312312
return p.checkerPool

internal/core/concurrency.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package core
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime"
7+
"strconv"
8+
"strings"
9+
"sync"
10+
11+
"github.com/microsoft/typescript-go/internal/testutil/race"
12+
)
13+
14+
type Concurrency struct {
15+
checkerCount int
16+
}
17+
18+
func ParseConcurrency(options *CompilerOptions) Concurrency {
19+
if options.SingleThreaded.IsTrue() {
20+
return Concurrency{
21+
checkerCount: 1,
22+
}
23+
}
24+
return parseConcurrency(options.Concurrency)
25+
}
26+
27+
func parseConcurrency(v string) Concurrency {
28+
checkerCount := 4
29+
30+
switch strings.ToLower(v) {
31+
case "default", "auto":
32+
break
33+
case "single", "none":
34+
checkerCount = 1
35+
case "max":
36+
checkerCount = runtime.GOMAXPROCS(0)
37+
case "half":
38+
checkerCount = max(1, runtime.GOMAXPROCS(0)/2)
39+
case "checker-per-file":
40+
checkerCount = -1
41+
default:
42+
if v, err := strconv.Atoi(v); err == nil && v > 0 {
43+
checkerCount = v
44+
}
45+
}
46+
47+
return Concurrency{
48+
checkerCount: checkerCount,
49+
}
50+
}
51+
52+
func (c Concurrency) SingleThreaded() bool {
53+
return c.checkerCount == 1
54+
}
55+
56+
func (c Concurrency) CheckerCount(numFiles int) int {
57+
if c.checkerCount == -1 {
58+
return max(1, numFiles)
59+
}
60+
return max(1, c.checkerCount)
61+
}
62+
63+
var testProgramConcurrency = sync.OnceValues(func() (concurrency Concurrency, raw string) {
64+
// Leave Program in SingleThreaded mode unless explicitly configured or in race mode.
65+
v := os.Getenv("TSGO_TEST_PROGRAM_CONCURRENCY")
66+
if v == "" && !race.Enabled {
67+
v = "single"
68+
}
69+
c := parseConcurrency(v)
70+
fmt.Println(c, v)
71+
return c, v
72+
})
73+
74+
func TestProgramConcurrency() (concurrency Concurrency, raw string) {
75+
return testProgramConcurrency()
76+
}

internal/testrunner/compiler_runner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ var concurrentSkippedErrorBaselines = collections.NewSetFromItems(
335335

336336
func (c *compilerTest) verifyDiagnostics(t *testing.T, suiteName string, isSubmodule bool) {
337337
t.Run("error", func(t *testing.T) {
338-
if !testutil.TestProgramIsSingleThreaded() && concurrentSkippedErrorBaselines.Has(c.testName) {
338+
concurrency, _ := core.TestProgramConcurrency()
339+
if !concurrency.SingleThreaded() && concurrentSkippedErrorBaselines.Has(c.testName) {
339340
t.Skip("Skipping error baseline in concurrent mode")
340341
}
341342

internal/testutil/harnessutil/harnessutil.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/microsoft/typescript-go/internal/repo"
2626
"github.com/microsoft/typescript-go/internal/scanner"
2727
"github.com/microsoft/typescript-go/internal/sourcemap"
28-
"github.com/microsoft/typescript-go/internal/testutil"
2928
"github.com/microsoft/typescript-go/internal/tsoptions"
3029
"github.com/microsoft/typescript-go/internal/tspath"
3130
"github.com/microsoft/typescript-go/internal/vfs"
@@ -182,9 +181,9 @@ func CompileFilesEx(
182181
compilerOptions.TypeRoots[i] = tspath.GetNormalizedAbsolutePath(typeRoot, currentDirectory)
183182
}
184183

185-
if compilerOptions.Concurrency == "" && compilerOptions.SingleThreaded.IsUnknown() && testutil.TestProgramIsSingleThreaded() {
186-
// TODO(jakebailey): replace TestProgramIsSingleThreaded with passed in value
187-
compilerOptions.Concurrency = "1"
184+
if compilerOptions.Concurrency == "" && compilerOptions.SingleThreaded.IsUnknown() {
185+
_, concurrency := core.TestProgramConcurrency()
186+
compilerOptions.Concurrency = concurrency
188187
}
189188

190189
// Create fake FS for testing

internal/testutil/testutil.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package testutil
22

33
import (
4-
"os"
54
"runtime/debug"
6-
"strconv"
7-
"sync"
85
"testing"
96

10-
"github.com/microsoft/typescript-go/internal/testutil/race"
117
"gotest.tools/v3/assert"
128
)
139

@@ -33,17 +29,3 @@ func RecoverAndFail(t *testing.T, msg string) {
3329
t.Fatalf("%s:\n%v\n%s", msg, r, string(stack))
3430
}
3531
}
36-
37-
var testProgramIsSingleThreaded = sync.OnceValue(func() bool {
38-
// Leave Program in SingleThreaded mode unless explicitly configured or in race mode.
39-
if v := os.Getenv("TS_TEST_PROGRAM_SINGLE_THREADED"); v != "" {
40-
if b, err := strconv.ParseBool(v); err == nil {
41-
return b
42-
}
43-
}
44-
return !race.Enabled
45-
})
46-
47-
func TestProgramIsSingleThreaded() bool {
48-
return testProgramIsSingleThreaded()
49-
}

0 commit comments

Comments
 (0)