Skip to content

Commit 1846133

Browse files
committed
modfile: add parsing support for toolchain
Add new toolchain directive to go.mod and go.work parser. Also fix error checking in parsing tests. For golang/go#57001. Change-Id: Ib7603f82cbd667f2152ed6b0c5989f08c28ceb1c Reviewed-on: https://go-review.googlesource.com/c/mod/+/497399 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent a73672d commit 1846133

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

modfile/read_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,13 @@ func TestPrintParse(t *testing.T) {
181181
pf1, err := Parse(base, data, nil)
182182
if err != nil {
183183
switch base {
184-
case "testdata/replace2.in", "testdata/gopkg.in.golden":
184+
case "testdata/block.golden",
185+
"testdata/block.in",
186+
"testdata/comment.golden",
187+
"testdata/comment.in",
188+
"testdata/rule1.golden":
189+
// ignore
190+
default:
185191
t.Errorf("should parse %v: %v", base, err)
186192
}
187193
}
@@ -425,28 +431,37 @@ func TestModulePath(t *testing.T) {
425431
}
426432
}
427433

428-
func TestGoVersion(t *testing.T) {
434+
func TestParseVersions(t *testing.T) {
429435
tests := []struct {
430436
desc, input string
431437
ok bool
432438
laxOK bool // ok=true implies laxOK=true; only set if ok=false
433439
}{
440+
// go lines
434441
{desc: "empty", input: "module m\ngo \n", ok: false},
435442
{desc: "one", input: "module m\ngo 1\n", ok: false},
436443
{desc: "two", input: "module m\ngo 1.22\n", ok: true},
437444
{desc: "three", input: "module m\ngo 1.22.333", ok: true},
438445
{desc: "before", input: "module m\ngo v1.2\n", ok: false},
439446
{desc: "after", input: "module m\ngo 1.2rc1\n", ok: true},
440447
{desc: "space", input: "module m\ngo 1.2 3.4\n", ok: false},
441-
{desc: "alt1", input: "module m\ngo 1.2.3\n", ok: true, laxOK: true},
442-
{desc: "alt2", input: "module m\ngo 1.2rc1\n", ok: true, laxOK: true},
443-
{desc: "alt3", input: "module m\ngo 1.2beta1\n", ok: true, laxOK: true},
448+
{desc: "alt1", input: "module m\ngo 1.2.3\n", ok: true},
449+
{desc: "alt2", input: "module m\ngo 1.2rc1\n", ok: true},
450+
{desc: "alt3", input: "module m\ngo 1.2beta1\n", ok: true},
444451
{desc: "alt4", input: "module m\ngo 1.2.beta1\n", ok: false, laxOK: true},
445452
{desc: "alt1", input: "module m\ngo v1.2.3\n", ok: false, laxOK: true},
446453
{desc: "alt2", input: "module m\ngo v1.2rc1\n", ok: false, laxOK: true},
447454
{desc: "alt3", input: "module m\ngo v1.2beta1\n", ok: false, laxOK: true},
448455
{desc: "alt4", input: "module m\ngo v1.2.beta1\n", ok: false, laxOK: true},
449456
{desc: "alt1", input: "module m\ngo v1.2\n", ok: false, laxOK: true},
457+
458+
// toolchain lines
459+
{desc: "tool", input: "module m\ntoolchain go1.2\n", ok: true},
460+
{desc: "tool1", input: "module m\ntoolchain go1.2.3\n", ok: true},
461+
{desc: "tool2", input: "module m\ntoolchain go1.2rc1\n", ok: true},
462+
{desc: "tool3", input: "module m\ntoolchain gccgo-go1.2rc1\n", ok: true},
463+
{desc: "tool4", input: "module m\ntoolchain local\n", ok: true},
464+
{desc: "tool5", input: "module m\ntoolchain inconceivable!\n", ok: false, laxOK: true},
450465
}
451466
t.Run("Strict", func(t *testing.T) {
452467
for _, test := range tests {
@@ -462,7 +477,7 @@ func TestGoVersion(t *testing.T) {
462477
t.Run("Lax", func(t *testing.T) {
463478
for _, test := range tests {
464479
t.Run(test.desc, func(t *testing.T) {
465-
if _, err := Parse("go.mod", []byte(test.input), nil); err == nil && !(test.ok || test.laxOK) {
480+
if _, err := ParseLax("go.mod", []byte(test.input), nil); err == nil && !(test.ok || test.laxOK) {
466481
t.Error("unexpected success")
467482
} else if err != nil && test.ok {
468483
t.Errorf("unexpected error: %v", err)

modfile/rule.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,21 @@ func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, a
375375
f.Go = &Go{Syntax: line}
376376
f.Go.Version = args[0]
377377

378+
case "toolchain":
379+
if f.Toolchain != nil {
380+
errorf("repeated toolchain statement")
381+
return
382+
}
383+
if len(args) != 1 {
384+
errorf("toolchain directive expects exactly one argument")
385+
return
386+
} else if strict && !ToolchainRE.MatchString(args[0]) {
387+
errorf("invalid toolchain version '%s': must match format go1.23 or local", args[0])
388+
return
389+
}
390+
f.Toolchain = &Toolchain{Syntax: line}
391+
f.Toolchain.Name = args[0]
392+
378393
case "module":
379394
if f.Module != nil {
380395
errorf("repeated module statement")
@@ -623,6 +638,22 @@ func (f *WorkFile) add(errs *ErrorList, line *Line, verb string, args []string,
623638
f.Go = &Go{Syntax: line}
624639
f.Go.Version = args[0]
625640

641+
case "toolchain":
642+
if f.Toolchain != nil {
643+
errorf("repeated toolchain statement")
644+
return
645+
}
646+
if len(args) != 1 {
647+
errorf("toolchain directive expects exactly one argument")
648+
return
649+
} else if !ToolchainRE.MatchString(args[0]) {
650+
errorf("invalid toolchain version '%s': must match format go1.23 or local", args[0])
651+
return
652+
}
653+
654+
f.Toolchain = &Toolchain{Syntax: line}
655+
f.Toolchain.Name = args[0]
656+
626657
case "use":
627658
if len(args) != 1 {
628659
errorf("usage: %s local/dir", verb)

modfile/testdata/goline.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go 1.2.3
2+
3+
toolchain local

modfile/testdata/goline.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go 1.2.3
2+
toolchain local

modfile/testdata/work/goline.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go 1.2.3
2+
3+
toolchain local

modfile/testdata/work/goline.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go 1.2.3
2+
toolchain local

modfile/work_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ func TestWorkPrintParse(t *testing.T) {
332332

333333
pf1, err := ParseWork(base, data, nil)
334334
if err != nil {
335-
switch base {
336-
case "testdata/replace2.in", "testdata/gopkg.in.golden":
337-
t.Errorf("should parse %v: %v", base, err)
338-
}
335+
t.Errorf("should parse %v: %v", base, err)
339336
}
340337
if err == nil {
341338
pf2, err := ParseWork(base, ndata, nil)

0 commit comments

Comments
 (0)