@@ -95,6 +95,8 @@ var updateGolden = flag.Bool("update", false, "if set, update test data during m
95
95
// for the test.
96
96
// - "settings.json": this file is parsed as JSON, and used as the
97
97
// session configuration (see gopls/doc/settings.md)
98
+ // - "env": this file is parsed as a list of VAR=VALUE fields specifying the
99
+ // editor environment.
98
100
// - Golden files: Within the archive, file names starting with '@' are
99
101
// treated as "golden" content, and are not written to disk, but instead are
100
102
// made available to test methods expecting an argument of type *Golden,
@@ -189,7 +191,6 @@ var updateGolden = flag.Bool("update", false, "if set, update test data during m
189
191
//
190
192
// Remaining TODO:
191
193
// - parallelize/optimize test execution
192
- // - add support for per-test environment?
193
194
// - reorganize regtest packages (and rename to just 'test'?)
194
195
//
195
196
// Existing marker tests to port:
@@ -246,9 +247,13 @@ func RunMarkerTests(t *testing.T, dir string) {
246
247
testenv .NeedsGo1Point (t , 18 )
247
248
}
248
249
test .executed = true
250
+ config := fake.EditorConfig {
251
+ Settings : test .settings ,
252
+ Env : test .env ,
253
+ }
249
254
c := & markerContext {
250
255
test : test ,
251
- env : newEnv (t , cache , test .files , test . settings ),
256
+ env : newEnv (t , cache , test .files , config ),
252
257
253
258
locations : make (map [expect.Identifier ]protocol.Location ),
254
259
diags : make (map [protocol.Location ][]protocol.Diagnostic ),
@@ -380,6 +385,7 @@ type MarkerTest struct {
380
385
fset * token.FileSet // fileset used for parsing notes
381
386
archive * txtar.Archive // original test archive
382
387
settings map [string ]interface {} // gopls settings
388
+ env map [string ]string // editor environment
383
389
files map [string ][]byte // data files from the archive (excluding special files)
384
390
notes []* expect.Note // extracted notes from data files
385
391
golden map [string ]* Golden // extracted golden content, by identifier name
@@ -490,6 +496,19 @@ func loadMarkerTest(name string, archive *txtar.Archive) (*MarkerTest, error) {
490
496
}
491
497
continue
492
498
}
499
+ if file .Name == "env" {
500
+ test .env = make (map [string ]string )
501
+ fields := strings .Fields (string (file .Data ))
502
+ for _ , field := range fields {
503
+ // TODO: use strings.Cut once we are on 1.18+.
504
+ idx := strings .IndexByte (field , '=' )
505
+ if idx < 0 {
506
+ return nil , fmt .Errorf ("env vars must be formatted as var=value, got %q" , field )
507
+ }
508
+ test .env [field [:idx ]] = field [idx + 1 :]
509
+ }
510
+ continue
511
+ }
493
512
if strings .HasPrefix (file .Name , "@" ) { // golden content
494
513
// TODO: use strings.Cut once we are on 1.18+.
495
514
idx := strings .IndexByte (file .Name , '/' )
@@ -541,6 +560,15 @@ func writeMarkerTests(dir string, tests []*MarkerTest) error {
541
560
}
542
561
arch .Files = append (arch .Files , txtar.File {Name : "settings.json" , Data : data })
543
562
}
563
+ if len (test .env ) > 0 {
564
+ var vars []string
565
+ for k , v := range test .env {
566
+ vars = append (vars , fmt .Sprintf ("%s=%s" , k , v ))
567
+ }
568
+ sort .Strings (vars )
569
+ data := []byte (strings .Join (vars , "\n " ))
570
+ arch .Files = append (arch .Files , txtar.File {Name : "env" , Data : data })
571
+ }
544
572
545
573
// ...followed by ordinary files. Preserve the order they appeared in the
546
574
// original archive.
@@ -578,7 +606,7 @@ func writeMarkerTests(dir string, tests []*MarkerTest) error {
578
606
//
579
607
// TODO(rfindley): simplify and refactor the construction of testing
580
608
// environments across regtests, marker tests, and benchmarks.
581
- func newEnv (t * testing.T , cache * cache.Cache , files map [string ][]byte , settings map [ string ] interface {} ) * Env {
609
+ func newEnv (t * testing.T , cache * cache.Cache , files map [string ][]byte , config fake. EditorConfig ) * Env {
582
610
sandbox , err := fake .NewSandbox (& fake.SandboxConfig {
583
611
RootDir : t .TempDir (),
584
612
GOPROXY : "https://proxy.golang.org" ,
@@ -596,9 +624,6 @@ func newEnv(t *testing.T, cache *cache.Cache, files map[string][]byte, settings
596
624
awaiter := NewAwaiter (sandbox .Workdir )
597
625
ss := lsprpc .NewStreamServer (cache , false , hooks .Options )
598
626
server := servertest .NewPipeServer (ss , jsonrpc2 .NewRawStream )
599
- config := fake.EditorConfig {
600
- Settings : settings ,
601
- }
602
627
editor , err := fake .NewEditor (sandbox , config ).Connect (ctx , server , awaiter .Hooks ())
603
628
if err != nil {
604
629
sandbox .Close () // ignore error
0 commit comments