modfile: rename directory directive to use For golang/go#48257 Change-Id: I9cbb222c93a066717595bec14ff26f2902ef05d9 Reviewed-on: https://go-review.googlesource.com/c/mod/+/359412 Trust: Michael Matloob <matloob@golang.org> Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
diff --git a/modfile/rule.go b/modfile/rule.go index 98211a4..ed2f31a 100644 --- a/modfile/rule.go +++ b/modfile/rule.go
@@ -609,7 +609,7 @@ f.Go = &Go{Syntax: line} f.Go.Version = args[0] - case "directory": + case "use": if len(args) != 1 { errorf("usage: %s local/dir", verb) return @@ -619,7 +619,7 @@ errorf("invalid quoted string: %v", err) return } - f.Directory = append(f.Directory, &Directory{ + f.Use = append(f.Use, &Use{ Path: s, Syntax: line, })
diff --git a/modfile/testdata/work/comment.golden b/modfile/testdata/work/comment.golden index 0a98a80..ac9bbbf 100644 --- a/modfile/testdata/work/comment.golden +++ b/modfile/testdata/work/comment.golden
@@ -1,10 +1,10 @@ // comment -directory x // eol +use x // eol // mid comment // comment 2 // comment 2 line 2 -directory y // eoy +use y // eoy // comment 3
diff --git a/modfile/testdata/work/comment.in b/modfile/testdata/work/comment.in index 2a016da..df6d702 100644 --- a/modfile/testdata/work/comment.in +++ b/modfile/testdata/work/comment.in
@@ -1,8 +1,8 @@ // comment -directory "x" // eol +use "x" // eol // mid comment // comment 2 // comment 2 line 2 -directory "y" // eoy +use "y" // eoy // comment 3
diff --git a/modfile/testdata/work/directory.golden b/modfile/testdata/work/directory.golden deleted file mode 100644 index 481b970..0000000 --- a/modfile/testdata/work/directory.golden +++ /dev/null
@@ -1,7 +0,0 @@ -directory ../foo - -directory ( - /bar - - baz -)
diff --git a/modfile/testdata/work/directory.in b/modfile/testdata/work/directory.in deleted file mode 100644 index eacfcaa..0000000 --- a/modfile/testdata/work/directory.in +++ /dev/null
@@ -1,7 +0,0 @@ -directory "../foo" - -directory ( - "/bar" - - "baz" -)
diff --git a/modfile/testdata/work/replace.golden b/modfile/testdata/work/replace.golden index b8e2bb5..534c1da 100644 --- a/modfile/testdata/work/replace.golden +++ b/modfile/testdata/work/replace.golden
@@ -1,4 +1,4 @@ -directory abc +use abc replace xyz v1.2.3 => /tmp/z
diff --git a/modfile/testdata/work/replace.in b/modfile/testdata/work/replace.in index aafc854..dc434d7 100644 --- a/modfile/testdata/work/replace.in +++ b/modfile/testdata/work/replace.in
@@ -1,4 +1,4 @@ -directory "abc" +use "abc" replace "xyz" v1.2.3 => "/tmp/z"
diff --git a/modfile/testdata/work/replace2.golden b/modfile/testdata/work/replace2.golden index 3d1546d..ca03dfd 100644 --- a/modfile/testdata/work/replace2.golden +++ b/modfile/testdata/work/replace2.golden
@@ -1,4 +1,4 @@ -directory abc +use abc replace ( xyz v1.2.3 => /tmp/z
diff --git a/modfile/testdata/work/replace2.in b/modfile/testdata/work/replace2.in index 0d3a8b7..cf34eb4 100644 --- a/modfile/testdata/work/replace2.in +++ b/modfile/testdata/work/replace2.in
@@ -1,4 +1,4 @@ -directory "abc" +use "abc" replace ( "xyz" v1.2.3 => "/tmp/z"
diff --git a/modfile/testdata/work/use.golden b/modfile/testdata/work/use.golden new file mode 100644 index 0000000..032bac1 --- /dev/null +++ b/modfile/testdata/work/use.golden
@@ -0,0 +1,7 @@ +use ../foo + +use ( + /bar + + baz +)
diff --git a/modfile/testdata/work/use.in b/modfile/testdata/work/use.in new file mode 100644 index 0000000..cb790dd --- /dev/null +++ b/modfile/testdata/work/use.in
@@ -0,0 +1,7 @@ +use "../foo" + +use ( + "/bar" + + "baz" +)
diff --git a/modfile/work.go b/modfile/work.go index b1fabff..0c0e521 100644 --- a/modfile/work.go +++ b/modfile/work.go
@@ -12,16 +12,16 @@ // A WorkFile is the parsed, interpreted form of a go.work file. type WorkFile struct { - Go *Go - Directory []*Directory - Replace []*Replace + Go *Go + Use []*Use + Replace []*Replace Syntax *FileSyntax } -// A Directory is a single directory statement. -type Directory struct { - Path string // Directory path of module. +// A Use is a single directory statement. +type Use struct { + Path string // Use path of module. ModulePath string // Module path in the comment. Syntax *Line } @@ -67,7 +67,7 @@ Err: fmt.Errorf("unknown block type: %s", strings.Join(x.Token, " ")), }) continue - case "directory", "replace": + case "use", "replace": for _, l := range x.Line { f.add(&errs, l, x.Token[0], l.Token, fix) } @@ -87,13 +87,13 @@ // Cleanup cleans out all the cleared entries. func (f *WorkFile) Cleanup() { w := 0 - for _, r := range f.Directory { + for _, r := range f.Use { if r.Path != "" { - f.Directory[w] = r + f.Use[w] = r w++ } } - f.Directory = f.Directory[:w] + f.Use = f.Use[:w] w = 0 for _, r := range f.Replace { @@ -133,60 +133,60 @@ return nil } -func (f *WorkFile) AddDirectory(diskPath, modulePath string) error { +func (f *WorkFile) AddUse(diskPath, modulePath string) error { need := true - for _, d := range f.Directory { + for _, d := range f.Use { if d.Path == diskPath { if need { d.ModulePath = modulePath - f.Syntax.updateLine(d.Syntax, "directory", AutoQuote(diskPath)) + f.Syntax.updateLine(d.Syntax, "use", AutoQuote(diskPath)) need = false } else { d.Syntax.markRemoved() - *d = Directory{} + *d = Use{} } } } if need { - f.AddNewDirectory(diskPath, modulePath) + f.AddNewUse(diskPath, modulePath) } return nil } -func (f *WorkFile) AddNewDirectory(diskPath, modulePath string) { - line := f.Syntax.addLine(nil, "directory", AutoQuote(diskPath)) - f.Directory = append(f.Directory, &Directory{Path: diskPath, ModulePath: modulePath, Syntax: line}) +func (f *WorkFile) AddNewUse(diskPath, modulePath string) { + line := f.Syntax.addLine(nil, "use", AutoQuote(diskPath)) + f.Use = append(f.Use, &Use{Path: diskPath, ModulePath: modulePath, Syntax: line}) } -func (f *WorkFile) SetDirectory(dirs []*Directory) { +func (f *WorkFile) SetUse(dirs []*Use) { need := make(map[string]string) for _, d := range dirs { need[d.Path] = d.ModulePath } - for _, d := range f.Directory { + for _, d := range f.Use { if modulePath, ok := need[d.Path]; ok { d.ModulePath = modulePath } else { d.Syntax.markRemoved() - *d = Directory{} + *d = Use{} } } // TODO(#45713): Add module path to comment. for diskPath, modulePath := range need { - f.AddNewDirectory(diskPath, modulePath) + f.AddNewUse(diskPath, modulePath) } f.SortBlocks() } -func (f *WorkFile) DropDirectory(path string) error { - for _, d := range f.Directory { +func (f *WorkFile) DropUse(path string) error { + for _, d := range f.Use { if d.Path == path { d.Syntax.markRemoved() - *d = Directory{} + *d = Use{} } } return nil
diff --git a/modfile/work_test.go b/modfile/work_test.go index 90f0f55..332df97 100644 --- a/modfile/work_test.go +++ b/modfile/work_test.go
@@ -12,8 +12,8 @@ "testing" ) -// TODO(#45713): Update these tests once AddDirectory sets the module path. -var workAddDirectoryTests = []struct { +// TODO(#45713): Update these tests once AddUse sets the module path. +var workAddUseTests = []struct { desc string in string path string @@ -24,7 +24,7 @@ `empty`, ``, `foo`, `bar`, - `directory foo`, + `use foo`, }, { `go_stmt_only`, @@ -32,32 +32,32 @@ `, `foo`, `bar`, `go 1.17 - directory foo + use foo `, }, { - `directory_line_present`, + `use_line_present`, `go 1.17 - directory baz`, + use baz`, `foo`, `bar`, `go 1.17 - directory ( + use ( baz foo ) `, }, { - `directory_block_present`, + `use_block_present`, `go 1.17 - directory ( + use ( baz quux ) `, `foo`, `bar`, `go 1.17 - directory ( + use ( baz quux foo @@ -65,14 +65,14 @@ `, }, { - `directory_and_replace_present`, + `use_and_replace_present`, `go 1.17 - directory baz + use baz replace a => ./b `, `foo`, `bar`, `go 1.17 - directory ( + use ( baz foo ) @@ -81,7 +81,7 @@ }, } -var workDropDirectoryTests = []struct { +var workDropUseTests = []struct { desc string in string path string @@ -102,46 +102,46 @@ `, }, { - `singled_directory`, + `single_use`, `go 1.17 - directory foo`, + use foo`, `foo`, `go 1.17 `, }, { - `directory_block`, + `use_block`, `go 1.17 - directory ( + use ( foo bar baz )`, `bar`, `go 1.17 - directory ( + use ( foo baz )`, }, { - `directory_multi`, + `use_multi`, `go 1.17 - directory ( + use ( foo bar baz ) - directory foo - directory quux - directory foo`, + use foo + use quux + use foo`, `foo`, `go 1.17 - directory ( + use ( bar baz ) - directory quux`, + use quux`, }, } @@ -167,38 +167,38 @@ go 1.17`, }, { - `directory_after_replace`, + `use_after_replace`, ` replace example.com/foo => ../bar - directory foo + use foo `, `1.17`, ` go 1.17 replace example.com/foo => ../bar - directory foo + use foo `, }, { - `directory_before_replace`, - `directory foo + `use_before_replace`, + `use foo replace example.com/foo => ../bar `, `1.17`, ` go 1.17 - directory foo + use foo replace example.com/foo => ../bar `, }, { - `directory_only`, - `directory foo + `use_only`, + `use foo `, `1.17`, ` go 1.17 - directory foo + use foo `, }, { @@ -216,24 +216,24 @@ desc, in, out string }{ { - `directory_duplicates_not_removed`, + `use_duplicates_not_removed`, `go 1.17 - directory foo - directory bar - directory ( + use foo + use bar + use ( foo )`, `go 1.17 - directory foo - directory bar - directory ( + use foo + use bar + use ( foo )`, }, { `replace_duplicates_removed`, `go 1.17 - directory foo + use foo replace x.y/z v1.0.0 => ./a replace x.y/z v1.1.0 => ./b replace ( @@ -241,7 +241,7 @@ ) `, `go 1.17 - directory foo + use foo replace x.y/z v1.1.0 => ./b replace ( x.y/z v1.0.0 => ./c @@ -250,21 +250,21 @@ }, } -func TestAddDirectory(t *testing.T) { - for _, tt := range workAddDirectoryTests { +func TestAddUse(t *testing.T) { + for _, tt := range workAddUseTests { t.Run(tt.desc, func(t *testing.T) { testWorkEdit(t, tt.in, tt.out, func(f *WorkFile) error { - return f.AddDirectory(tt.path, tt.modulePath) + return f.AddUse(tt.path, tt.modulePath) }) }) } } -func TestDropDirectory(t *testing.T) { - for _, tt := range workDropDirectoryTests { +func TestDropUse(t *testing.T) { + for _, tt := range workDropUseTests { t.Run(tt.desc, func(t *testing.T) { testWorkEdit(t, tt.in, tt.out, func(f *WorkFile) error { - if err := f.DropDirectory(tt.path); err != nil { + if err := f.DropUse(tt.path); err != nil { return err } f.Cleanup()