Skip to content

Commit fc83a8f

Browse files
committed
modfile: add support for dropping go and toolchain stmts
Also add tests of previous CLs. For golang/go#57001. Change-Id: I755429dd07c0e84910108ce9807d607115329b79 Reviewed-on: https://go-review.googlesource.com/c/mod/+/497400 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 1846133 commit fc83a8f

File tree

5 files changed

+268
-6
lines changed

5 files changed

+268
-6
lines changed

modfile/print.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import (
1616
func Format(f *FileSyntax) []byte {
1717
pr := &printer{}
1818
pr.file(f)
19-
return pr.Bytes()
19+
20+
// remove trailing blank lines
21+
b := pr.Bytes()
22+
for len(b) > 0 && b[len(b)-1] == '\n' && (len(b) == 1 || b[len(b)-2] == '\n') {
23+
b = b[:len(b)-1]
24+
}
25+
return b
2026
}
2127

2228
// A printer collects the state during printing of a file or expression.
@@ -59,7 +65,11 @@ func (p *printer) newline() {
5965
}
6066

6167
p.trim()
62-
p.printf("\n")
68+
if b := p.Bytes(); len(b) == 0 || (len(b) >= 2 && b[len(b)-1] == '\n' && b[len(b)-2] == '\n') {
69+
// skip the blank line at top of file or after a blank line
70+
} else {
71+
p.printf("\n")
72+
}
6373
for i := 0; i < p.margin; i++ {
6474
p.printf("\t")
6575
}

modfile/rule.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,22 @@ func (f *File) AddGoStmt(version string) error {
986986
return nil
987987
}
988988

989+
// DropGoStmt deletes the go statement from the file.
990+
func (f *File) DropGoStmt() {
991+
if f.Go != nil {
992+
f.Go.Syntax.markRemoved()
993+
f.Go = nil
994+
}
995+
}
996+
997+
// DropToolchainStmt deletes the toolchain statement from the file.
998+
func (f *File) DropToolchainStmt() {
999+
if f.Toolchain != nil {
1000+
f.Toolchain.Syntax.markRemoved()
1001+
f.Toolchain = nil
1002+
}
1003+
}
1004+
9891005
func (f *File) AddToolchainStmt(name string) error {
9901006
if !ToolchainRE.MatchString(name) {
9911007
return fmt.Errorf("invalid toolchain name %q", name)
@@ -1003,7 +1019,7 @@ func (f *File) AddToolchainStmt(name string) error {
10031019
}
10041020
} else {
10051021
f.Toolchain.Name = name
1006-
f.Syntax.updateLine(f.Go.Syntax, "toolchain", name)
1022+
f.Syntax.updateLine(f.Toolchain.Syntax, "toolchain", name)
10071023
}
10081024
return nil
10091025
}

modfile/rule_test.go

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,137 @@ var addGoTests = []struct {
696696
},
697697
}
698698

699+
var dropGoTests = []struct {
700+
desc string
701+
in string
702+
out string
703+
}{
704+
{
705+
`module_only`,
706+
`module m
707+
go 1.14
708+
`,
709+
`module m
710+
`,
711+
},
712+
{
713+
`module_before_require`,
714+
`module m
715+
go 1.14
716+
require x.y/a v1.2.3
717+
`,
718+
`module m
719+
require x.y/a v1.2.3
720+
`,
721+
},
722+
{
723+
`require_before_module`,
724+
`require x.y/a v1.2.3
725+
module example.com/inverted
726+
go 1.14
727+
`,
728+
`require x.y/a v1.2.3
729+
module example.com/inverted
730+
`,
731+
},
732+
{
733+
`require_only`,
734+
`require x.y/a v1.2.3
735+
go 1.14
736+
`,
737+
`require x.y/a v1.2.3
738+
`,
739+
},
740+
}
741+
742+
var addToolchainTests = []struct {
743+
desc string
744+
in string
745+
version string
746+
out string
747+
}{
748+
{
749+
`empty`,
750+
``,
751+
`go1.17`,
752+
`toolchain go1.17
753+
`,
754+
},
755+
{
756+
`aftergo`,
757+
`// this is a comment
758+
require x v1.0.0
759+
760+
go 1.17
761+
762+
require y v1.0.0
763+
`,
764+
`go1.17`,
765+
`// this is a comment
766+
require x v1.0.0
767+
768+
go 1.17
769+
770+
toolchain go1.17
771+
772+
require y v1.0.0
773+
`,
774+
},
775+
{
776+
`already_have_toolchain`,
777+
`go 1.17
778+
779+
toolchain go1.18
780+
`,
781+
`go1.19`,
782+
`go 1.17
783+
784+
toolchain go1.19
785+
`,
786+
},
787+
}
788+
789+
var dropToolchainTests = []struct {
790+
desc string
791+
in string
792+
out string
793+
}{
794+
{
795+
`empty`,
796+
`toolchain go1.17
797+
`,
798+
``,
799+
},
800+
{
801+
`aftergo`,
802+
`// this is a comment
803+
require x v1.0.0
804+
805+
go 1.17
806+
807+
toolchain go1.17
808+
809+
require y v1.0.0
810+
`,
811+
`// this is a comment
812+
require x v1.0.0
813+
814+
go 1.17
815+
816+
require y v1.0.0
817+
`,
818+
},
819+
{
820+
`already_have_toolchain`,
821+
`go 1.17
822+
823+
toolchain go1.18
824+
`,
825+
`go 1.17
826+
`,
827+
},
828+
}
829+
699830
var addExcludeTests = []struct {
700831
desc string
701832
in string
@@ -948,7 +1079,7 @@ var retractRationaleTests = []struct {
9481079
`prefix_one`,
9491080
`module m
9501081
// prefix
951-
retract v1.0.0
1082+
retract v1.0.0
9521083
`,
9531084
`prefix`,
9541085
},
@@ -959,7 +1090,7 @@ var retractRationaleTests = []struct {
9591090
//
9601091
// two
9611092
//
962-
// three
1093+
// three
9631094
retract v1.0.0`,
9641095
`one
9651096
@@ -1083,7 +1214,7 @@ var moduleDeprecatedTests = []struct {
10831214
{
10841215
`deprecated_paragraph_space`,
10851216
`// Deprecated: the next line has a space
1086-
//
1217+
//
10871218
// c
10881219
module m`,
10891220
"the next line has a space",
@@ -1501,6 +1632,38 @@ func TestAddGo(t *testing.T) {
15011632
}
15021633
}
15031634

1635+
func TestDropGo(t *testing.T) {
1636+
for _, tt := range dropGoTests {
1637+
t.Run(tt.desc, func(t *testing.T) {
1638+
testEdit(t, tt.in, tt.out, true, func(f *File) error {
1639+
f.DropGoStmt()
1640+
return nil
1641+
})
1642+
})
1643+
}
1644+
}
1645+
1646+
func TestAddToolchain(t *testing.T) {
1647+
for _, tt := range addToolchainTests {
1648+
t.Run(tt.desc, func(t *testing.T) {
1649+
testEdit(t, tt.in, tt.out, true, func(f *File) error {
1650+
return f.AddToolchainStmt(tt.version)
1651+
})
1652+
})
1653+
}
1654+
}
1655+
1656+
func TestDropToolchain(t *testing.T) {
1657+
for _, tt := range dropToolchainTests {
1658+
t.Run(tt.desc, func(t *testing.T) {
1659+
testEdit(t, tt.in, tt.out, true, func(f *File) error {
1660+
f.DropToolchainStmt()
1661+
return nil
1662+
})
1663+
})
1664+
}
1665+
}
1666+
15041667
func TestAddExclude(t *testing.T) {
15051668
for _, tt := range addExcludeTests {
15061669
t.Run(tt.desc, func(t *testing.T) {

modfile/work.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,22 @@ func (f *WorkFile) AddToolchainStmt(name string) error {
168168
return nil
169169
}
170170

171+
// DropGoStmt deletes the go statement from the file.
172+
func (f *WorkFile) DropGoStmt() {
173+
if f.Go != nil {
174+
f.Go.Syntax.markRemoved()
175+
f.Go = nil
176+
}
177+
}
178+
179+
// DropToolchainStmt deletes the toolchain statement from the file.
180+
func (f *WorkFile) DropToolchainStmt() {
181+
if f.Toolchain != nil {
182+
f.Toolchain.Syntax.markRemoved()
183+
f.Toolchain = nil
184+
}
185+
}
186+
171187
func (f *WorkFile) AddUse(diskPath, modulePath string) error {
172188
need := true
173189
for _, d := range f.Use {

modfile/work_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,53 @@ var workAddGoTests = []struct {
212212
},
213213
}
214214

215+
var workAddToolchainTests = []struct {
216+
desc string
217+
in string
218+
version string
219+
out string
220+
}{
221+
{
222+
`empty`,
223+
``,
224+
`go1.17`,
225+
`toolchain go1.17
226+
`,
227+
},
228+
{
229+
`aftergo`,
230+
`// this is a comment
231+
use foo
232+
233+
go 1.17
234+
235+
use bar
236+
`,
237+
`go1.17`,
238+
`// this is a comment
239+
use foo
240+
241+
go 1.17
242+
243+
toolchain go1.17
244+
245+
use bar
246+
`,
247+
},
248+
{
249+
`already_have_toolchain`,
250+
`go 1.17
251+
252+
toolchain go1.18
253+
`,
254+
`go1.19`,
255+
`go 1.17
256+
257+
toolchain go1.19
258+
`,
259+
},
260+
}
261+
215262
var workSortBlocksTests = []struct {
216263
desc, in, out string
217264
}{
@@ -284,6 +331,16 @@ func TestWorkAddGo(t *testing.T) {
284331
}
285332
}
286333

334+
func TestWorkAddToolchain(t *testing.T) {
335+
for _, tt := range workAddToolchainTests {
336+
t.Run(tt.desc, func(t *testing.T) {
337+
testWorkEdit(t, tt.in, tt.out, func(f *WorkFile) error {
338+
return f.AddToolchainStmt(tt.version)
339+
})
340+
})
341+
}
342+
}
343+
287344
func TestWorkSortBlocks(t *testing.T) {
288345
for _, tt := range workSortBlocksTests {
289346
t.Run(tt.desc, func(t *testing.T) {

0 commit comments

Comments
 (0)