[dev.cmdgo] modfile: parser changes for workfile proposal

This change adds interfaces to the mod package to support parsing
and representing go.work files, including WorkFile for the file as a
whole, Directory for directory statements, and ParseWork for parsing
go.work files.

This code is mostly a lightly modified version of the parsing
code for mod files.

This is meant to support the workspaces proposal and if the proposal
is accepted it's expected theinterface will likely change.

For #45713

Change-Id: I5df6fe4acba1dbe86bc3e3fba40a04fbb4d678e4
Reviewed-on: https://go-review.googlesource.com/c/mod/+/336089
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
diff --git a/modfile/read_test.go b/modfile/read_test.go
index fea0d5d..e43f693 100644
--- a/modfile/read_test.go
+++ b/modfile/read_test.go
@@ -152,6 +152,9 @@
 	for _, out := range outs {
 		out := out
 		name := filepath.Base(out)
+		if !strings.HasSuffix(out, ".in") && !strings.HasSuffix(out, ".golden") {
+			continue
+		}
 		t.Run(name, func(t *testing.T) {
 			t.Parallel()
 			data, err := ioutil.ReadFile(out)
diff --git a/modfile/rule.go b/modfile/rule.go
index 78f83fa..d6a2d38 100644
--- a/modfile/rule.go
+++ b/modfile/rule.go
@@ -423,68 +423,12 @@
 		}
 
 	case "replace":
-		arrow := 2
-		if len(args) >= 2 && args[1] == "=>" {
-			arrow = 1
-		}
-		if len(args) < arrow+2 || len(args) > arrow+3 || args[arrow] != "=>" {
-			errorf("usage: %s module/path [v1.2.3] => other/module v1.4\n\t or %s module/path [v1.2.3] => ../local/directory", verb, verb)
+		replace, wrappederr := parseReplace(f.Syntax.Name, line, verb, args, fix)
+		if wrappederr != nil {
+			*errs = append(*errs, *wrappederr)
 			return
 		}
-		s, err := parseString(&args[0])
-		if err != nil {
-			errorf("invalid quoted string: %v", err)
-			return
-		}
-		pathMajor, err := modulePathMajor(s)
-		if err != nil {
-			wrapModPathError(s, err)
-			return
-		}
-		var v string
-		if arrow == 2 {
-			v, err = parseVersion(verb, s, &args[1], fix)
-			if err != nil {
-				wrapError(err)
-				return
-			}
-			if err := module.CheckPathMajor(v, pathMajor); err != nil {
-				wrapModPathError(s, err)
-				return
-			}
-		}
-		ns, err := parseString(&args[arrow+1])
-		if err != nil {
-			errorf("invalid quoted string: %v", err)
-			return
-		}
-		nv := ""
-		if len(args) == arrow+2 {
-			if !IsDirectoryPath(ns) {
-				errorf("replacement module without version must be directory path (rooted or starting with ./ or ../)")
-				return
-			}
-			if filepath.Separator == '/' && strings.Contains(ns, `\`) {
-				errorf("replacement directory appears to be Windows path (on a non-windows system)")
-				return
-			}
-		}
-		if len(args) == arrow+3 {
-			nv, err = parseVersion(verb, ns, &args[arrow+2], fix)
-			if err != nil {
-				wrapError(err)
-				return
-			}
-			if IsDirectoryPath(ns) {
-				errorf("replacement module directory path %q cannot have version", ns)
-				return
-			}
-		}
-		f.Replace = append(f.Replace, &Replace{
-			Old:    module.Version{Path: s, Version: v},
-			New:    module.Version{Path: ns, Version: nv},
-			Syntax: line,
-		})
+		f.Replace = append(f.Replace, replace)
 
 	case "retract":
 		rationale := parseDirectiveComment(block, line)
@@ -515,6 +459,83 @@
 	}
 }
 
+func parseReplace(filename string, line *Line, verb string, args []string, fix VersionFixer) (*Replace, *Error) {
+	wrapModPathError := func(modPath string, err error) *Error {
+		return &Error{
+			Filename: filename,
+			Pos:      line.Start,
+			ModPath:  modPath,
+			Verb:     verb,
+			Err:      err,
+		}
+	}
+	wrapError := func(err error) *Error {
+		return &Error{
+			Filename: filename,
+			Pos:      line.Start,
+			Err:      err,
+		}
+	}
+	errorf := func(format string, args ...interface{}) *Error {
+		return wrapError(fmt.Errorf(format, args...))
+	}
+
+	arrow := 2
+	if len(args) >= 2 && args[1] == "=>" {
+		arrow = 1
+	}
+	if len(args) < arrow+2 || len(args) > arrow+3 || args[arrow] != "=>" {
+		return nil, errorf("usage: %s module/path [v1.2.3] => other/module v1.4\n\t or %s module/path [v1.2.3] => ../local/directory", verb, verb)
+	}
+	s, err := parseString(&args[0])
+	if err != nil {
+		return nil, errorf("invalid quoted string: %v", err)
+	}
+	pathMajor, err := modulePathMajor(s)
+	if err != nil {
+		return nil, wrapModPathError(s, err)
+
+	}
+	var v string
+	if arrow == 2 {
+		v, err = parseVersion(verb, s, &args[1], fix)
+		if err != nil {
+			return nil, wrapError(err)
+		}
+		if err := module.CheckPathMajor(v, pathMajor); err != nil {
+			return nil, wrapModPathError(s, err)
+		}
+	}
+	ns, err := parseString(&args[arrow+1])
+	if err != nil {
+		return nil, errorf("invalid quoted string: %v", err)
+	}
+	nv := ""
+	if len(args) == arrow+2 {
+		if !IsDirectoryPath(ns) {
+			return nil, errorf("replacement module without version must be directory path (rooted or starting with ./ or ../)")
+		}
+		if filepath.Separator == '/' && strings.Contains(ns, `\`) {
+			return nil, errorf("replacement directory appears to be Windows path (on a non-windows system)")
+		}
+	}
+	if len(args) == arrow+3 {
+		nv, err = parseVersion(verb, ns, &args[arrow+2], fix)
+		if err != nil {
+			return nil, wrapError(err)
+		}
+		if IsDirectoryPath(ns) {
+			return nil, errorf("replacement module directory path %q cannot have version", ns)
+
+		}
+	}
+	return &Replace{
+		Old:    module.Version{Path: s, Version: v},
+		New:    module.Version{Path: ns, Version: nv},
+		Syntax: line,
+	}, nil
+}
+
 // fixRetract applies fix to each retract directive in f, appending any errors
 // to errs.
 //
@@ -556,6 +577,63 @@
 	}
 }
 
+func (f *WorkFile) add(errs *ErrorList, line *Line, verb string, args []string, fix VersionFixer) {
+	wrapError := func(err error) {
+		*errs = append(*errs, Error{
+			Filename: f.Syntax.Name,
+			Pos:      line.Start,
+			Err:      err,
+		})
+	}
+	errorf := func(format string, args ...interface{}) {
+		wrapError(fmt.Errorf(format, args...))
+	}
+
+	switch verb {
+	default:
+		errorf("unknown directive: %s", verb)
+
+	case "go":
+		if f.Go != nil {
+			errorf("repeated go statement")
+			return
+		}
+		if len(args) != 1 {
+			errorf("go directive expects exactly one argument")
+			return
+		} else if !GoVersionRE.MatchString(args[0]) {
+			errorf("invalid go version '%s': must match format 1.23", args[0])
+			return
+		}
+
+		f.Go = &Go{Syntax: line}
+		f.Go.Version = args[0]
+
+	case "directory":
+		if len(args) != 1 {
+			errorf("usage: %s local/dir", verb)
+			return
+		}
+		s, err := parseString(&args[0])
+		if err != nil {
+			errorf("invalid quoted string: %v", err)
+			return
+		}
+		f.Directory = append(f.Directory, &Directory{
+			Path:   s,
+			Syntax: line,
+		})
+
+	case "replace":
+		replace, wrappederr := parseReplace(f.Syntax.Name, line, verb, args, fix)
+		if wrappederr != nil {
+			*errs = append(*errs, *wrappederr)
+			return
+		}
+		f.Replace = append(f.Replace, replace)
+	}
+}
+
 // IsDirectoryPath reports whether the given path should be interpreted
 // as a directory path. Just like on the go command line, relative paths
 // and rooted paths are directory paths; the rest are module paths.
@@ -1165,6 +1243,10 @@
 }
 
 func (f *File) AddReplace(oldPath, oldVers, newPath, newVers string) error {
+	return addReplace(f.Syntax, &f.Replace, oldPath, oldVers, newPath, newVers)
+}
+
+func addReplace(syntax *FileSyntax, replace *[]*Replace, oldPath, oldVers, newPath, newVers string) error {
 	need := true
 	old := module.Version{Path: oldPath, Version: oldVers}
 	new := module.Version{Path: newPath, Version: newVers}
@@ -1178,12 +1260,12 @@
 	}
 
 	var hint *Line
-	for _, r := range f.Replace {
+	for _, r := range *replace {
 		if r.Old.Path == oldPath && (oldVers == "" || r.Old.Version == oldVers) {
 			if need {
 				// Found replacement for old; update to use new.
 				r.New = new
-				f.Syntax.updateLine(r.Syntax, tokens...)
+				syntax.updateLine(r.Syntax, tokens...)
 				need = false
 				continue
 			}
@@ -1196,7 +1278,7 @@
 		}
 	}
 	if need {
-		f.Replace = append(f.Replace, &Replace{Old: old, New: new, Syntax: f.Syntax.addLine(hint, tokens...)})
+		*replace = append(*replace, &Replace{Old: old, New: new, Syntax: syntax.addLine(hint, tokens...)})
 	}
 	return nil
 }
@@ -1282,30 +1364,36 @@
 // retract directives are not de-duplicated since comments are
 // meaningful, and versions may be retracted multiple times.
 func (f *File) removeDups() {
+	removeDups(f.Syntax, &f.Exclude, &f.Replace)
+}
+
+func removeDups(syntax *FileSyntax, exclude *[]*Exclude, replace *[]*Replace) {
 	kill := make(map[*Line]bool)
 
 	// Remove duplicate excludes.
-	haveExclude := make(map[module.Version]bool)
-	for _, x := range f.Exclude {
-		if haveExclude[x.Mod] {
-			kill[x.Syntax] = true
-			continue
+	if exclude != nil {
+		haveExclude := make(map[module.Version]bool)
+		for _, x := range *exclude {
+			if haveExclude[x.Mod] {
+				kill[x.Syntax] = true
+				continue
+			}
+			haveExclude[x.Mod] = true
 		}
-		haveExclude[x.Mod] = true
-	}
-	var excl []*Exclude
-	for _, x := range f.Exclude {
-		if !kill[x.Syntax] {
-			excl = append(excl, x)
+		var excl []*Exclude
+		for _, x := range *exclude {
+			if !kill[x.Syntax] {
+				excl = append(excl, x)
+			}
 		}
+		*exclude = excl
 	}
-	f.Exclude = excl
 
 	// Remove duplicate replacements.
 	// Later replacements take priority over earlier ones.
 	haveReplace := make(map[module.Version]bool)
-	for i := len(f.Replace) - 1; i >= 0; i-- {
-		x := f.Replace[i]
+	for i := len(*replace) - 1; i >= 0; i-- {
+		x := (*replace)[i]
 		if haveReplace[x.Old] {
 			kill[x.Syntax] = true
 			continue
@@ -1313,18 +1401,18 @@
 		haveReplace[x.Old] = true
 	}
 	var repl []*Replace
-	for _, x := range f.Replace {
+	for _, x := range *replace {
 		if !kill[x.Syntax] {
 			repl = append(repl, x)
 		}
 	}
-	f.Replace = repl
+	*replace = repl
 
 	// Duplicate require and retract directives are not removed.
 
 	// Drop killed statements from the syntax tree.
 	var stmts []Expr
-	for _, stmt := range f.Syntax.Stmt {
+	for _, stmt := range syntax.Stmt {
 		switch stmt := stmt.(type) {
 		case *Line:
 			if kill[stmt] {
@@ -1344,7 +1432,7 @@
 		}
 		stmts = append(stmts, stmt)
 	}
-	f.Syntax.Stmt = stmts
+	syntax.Stmt = stmts
 }
 
 // lineLess returns whether li should be sorted before lj. It sorts
diff --git a/modfile/testdata/work/comment.golden b/modfile/testdata/work/comment.golden
new file mode 100644
index 0000000..0a98a80
--- /dev/null
+++ b/modfile/testdata/work/comment.golden
@@ -0,0 +1,10 @@
+// comment
+directory x // eol
+
+// mid comment
+
+// comment 2
+// comment 2 line 2
+directory y // eoy
+
+// comment 3
diff --git a/modfile/testdata/work/comment.in b/modfile/testdata/work/comment.in
new file mode 100644
index 0000000..2a016da
--- /dev/null
+++ b/modfile/testdata/work/comment.in
@@ -0,0 +1,8 @@
+// comment
+directory "x" // eol
+// mid comment
+
+// comment 2
+// comment 2 line 2
+directory "y" // eoy
+// comment 3
diff --git a/modfile/testdata/work/directory.golden b/modfile/testdata/work/directory.golden
new file mode 100644
index 0000000..481b970
--- /dev/null
+++ b/modfile/testdata/work/directory.golden
@@ -0,0 +1,7 @@
+directory ../foo
+
+directory (
+	/bar
+
+	baz
+)
diff --git a/modfile/testdata/work/directory.in b/modfile/testdata/work/directory.in
new file mode 100644
index 0000000..eacfcaa
--- /dev/null
+++ b/modfile/testdata/work/directory.in
@@ -0,0 +1,7 @@
+directory "../foo"
+
+directory (
+	"/bar"
+
+	"baz"
+)
diff --git a/modfile/testdata/work/empty.golden b/modfile/testdata/work/empty.golden
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/modfile/testdata/work/empty.golden
diff --git a/modfile/testdata/work/empty.in b/modfile/testdata/work/empty.in
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/modfile/testdata/work/empty.in
diff --git a/modfile/testdata/work/replace.golden b/modfile/testdata/work/replace.golden
new file mode 100644
index 0000000..b8e2bb5
--- /dev/null
+++ b/modfile/testdata/work/replace.golden
@@ -0,0 +1,12 @@
+directory abc
+
+replace xyz v1.2.3 => /tmp/z
+
+replace xyz v1.3.4 => my/xyz v1.3.4-me
+
+replace (
+	w v1.0.0 => "./a,"
+	w v1.0.1 => "./a()"
+	w v1.0.2 => "./a[]"
+	w v1.0.3 => "./a{}"
+)
diff --git a/modfile/testdata/work/replace.in b/modfile/testdata/work/replace.in
new file mode 100644
index 0000000..aafc854
--- /dev/null
+++ b/modfile/testdata/work/replace.in
@@ -0,0 +1,12 @@
+directory "abc"
+
+replace "xyz" v1.2.3 => "/tmp/z"
+
+replace "xyz" v1.3.4 => "my/xyz" v1.3.4-me
+
+replace (
+	"w" v1.0.0 => "./a,"
+	"w" v1.0.1 => "./a()"
+	"w" v1.0.2 => "./a[]"
+	"w" v1.0.3 => "./a{}"
+)
diff --git a/modfile/testdata/work/replace2.golden b/modfile/testdata/work/replace2.golden
new file mode 100644
index 0000000..3d1546d
--- /dev/null
+++ b/modfile/testdata/work/replace2.golden
@@ -0,0 +1,10 @@
+directory abc
+
+replace (
+	xyz v1.2.3 => /tmp/z
+	xyz v1.3.4 => my/xyz v1.3.4-me
+	xyz v1.4.5 => "/tmp/my dir"
+	xyz v1.5.6 => my/xyz v1.5.6
+
+	xyz => my/other/xyz v1.5.4
+)
diff --git a/modfile/testdata/work/replace2.in b/modfile/testdata/work/replace2.in
new file mode 100644
index 0000000..0d3a8b7
--- /dev/null
+++ b/modfile/testdata/work/replace2.in
@@ -0,0 +1,10 @@
+directory "abc"
+
+replace (
+	"xyz" v1.2.3 => "/tmp/z"
+	"xyz" v1.3.4 => "my/xyz" "v1.3.4-me"
+	xyz "v1.4.5" => "/tmp/my dir"
+	xyz v1.5.6 => my/xyz v1.5.6
+
+	xyz => my/other/xyz v1.5.4
+)
diff --git a/modfile/work.go b/modfile/work.go
new file mode 100644
index 0000000..b1fabff
--- /dev/null
+++ b/modfile/work.go
@@ -0,0 +1,234 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modfile
+
+import (
+	"fmt"
+	"sort"
+	"strings"
+)
+
+// A WorkFile is the parsed, interpreted form of a go.work file.
+type WorkFile struct {
+	Go        *Go
+	Directory []*Directory
+	Replace   []*Replace
+
+	Syntax *FileSyntax
+}
+
+// A Directory is a single directory statement.
+type Directory struct {
+	Path       string // Directory path of module.
+	ModulePath string // Module path in the comment.
+	Syntax     *Line
+}
+
+// ParseWork parses and returns a go.work file.
+//
+// file is the name of the file, used in positions and errors.
+//
+// data is the content of the file.
+//
+// fix is an optional function that canonicalizes module versions.
+// If fix is nil, all module versions must be canonical (module.CanonicalVersion
+// must return the same string).
+func ParseWork(file string, data []byte, fix VersionFixer) (*WorkFile, error) {
+	fs, err := parse(file, data)
+	if err != nil {
+		return nil, err
+	}
+	f := &WorkFile{
+		Syntax: fs,
+	}
+	var errs ErrorList
+
+	for _, x := range fs.Stmt {
+		switch x := x.(type) {
+		case *Line:
+			f.add(&errs, x, x.Token[0], x.Token[1:], fix)
+
+		case *LineBlock:
+			if len(x.Token) > 1 {
+				errs = append(errs, Error{
+					Filename: file,
+					Pos:      x.Start,
+					Err:      fmt.Errorf("unknown block type: %s", strings.Join(x.Token, " ")),
+				})
+				continue
+			}
+			switch x.Token[0] {
+			default:
+				errs = append(errs, Error{
+					Filename: file,
+					Pos:      x.Start,
+					Err:      fmt.Errorf("unknown block type: %s", strings.Join(x.Token, " ")),
+				})
+				continue
+			case "directory", "replace":
+				for _, l := range x.Line {
+					f.add(&errs, l, x.Token[0], l.Token, fix)
+				}
+			}
+		}
+	}
+
+	if len(errs) > 0 {
+		return nil, errs
+	}
+	return f, nil
+}
+
+// Cleanup cleans up the file f after any edit operations.
+// To avoid quadratic behavior, modifications like DropRequire
+// clear the entry but do not remove it from the slice.
+// Cleanup cleans out all the cleared entries.
+func (f *WorkFile) Cleanup() {
+	w := 0
+	for _, r := range f.Directory {
+		if r.Path != "" {
+			f.Directory[w] = r
+			w++
+		}
+	}
+	f.Directory = f.Directory[:w]
+
+	w = 0
+	for _, r := range f.Replace {
+		if r.Old.Path != "" {
+			f.Replace[w] = r
+			w++
+		}
+	}
+	f.Replace = f.Replace[:w]
+
+	f.Syntax.Cleanup()
+}
+
+func (f *WorkFile) AddGoStmt(version string) error {
+	if !GoVersionRE.MatchString(version) {
+		return fmt.Errorf("invalid language version string %q", version)
+	}
+	if f.Go == nil {
+		stmt := &Line{Token: []string{"go", version}}
+		f.Go = &Go{
+			Version: version,
+			Syntax:  stmt,
+		}
+		// Find the first non-comment-only block that's and add
+		// the go statement before it. That will keep file comments at the top.
+		i := 0
+		for i = 0; i < len(f.Syntax.Stmt); i++ {
+			if _, ok := f.Syntax.Stmt[i].(*CommentBlock); !ok {
+				break
+			}
+		}
+		f.Syntax.Stmt = append(append(f.Syntax.Stmt[:i:i], stmt), f.Syntax.Stmt[i:]...)
+	} else {
+		f.Go.Version = version
+		f.Syntax.updateLine(f.Go.Syntax, "go", version)
+	}
+	return nil
+}
+
+func (f *WorkFile) AddDirectory(diskPath, modulePath string) error {
+	need := true
+	for _, d := range f.Directory {
+		if d.Path == diskPath {
+			if need {
+				d.ModulePath = modulePath
+				f.Syntax.updateLine(d.Syntax, "directory", AutoQuote(diskPath))
+				need = false
+			} else {
+				d.Syntax.markRemoved()
+				*d = Directory{}
+			}
+		}
+	}
+
+	if need {
+		f.AddNewDirectory(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) SetDirectory(dirs []*Directory) {
+	need := make(map[string]string)
+	for _, d := range dirs {
+		need[d.Path] = d.ModulePath
+	}
+
+	for _, d := range f.Directory {
+		if modulePath, ok := need[d.Path]; ok {
+			d.ModulePath = modulePath
+		} else {
+			d.Syntax.markRemoved()
+			*d = Directory{}
+		}
+	}
+
+	// TODO(#45713): Add module path to comment.
+
+	for diskPath, modulePath := range need {
+		f.AddNewDirectory(diskPath, modulePath)
+	}
+	f.SortBlocks()
+}
+
+func (f *WorkFile) DropDirectory(path string) error {
+	for _, d := range f.Directory {
+		if d.Path == path {
+			d.Syntax.markRemoved()
+			*d = Directory{}
+		}
+	}
+	return nil
+}
+
+func (f *WorkFile) AddReplace(oldPath, oldVers, newPath, newVers string) error {
+	return addReplace(f.Syntax, &f.Replace, oldPath, oldVers, newPath, newVers)
+}
+
+func (f *WorkFile) DropReplace(oldPath, oldVers string) error {
+	for _, r := range f.Replace {
+		if r.Old.Path == oldPath && r.Old.Version == oldVers {
+			r.Syntax.markRemoved()
+			*r = Replace{}
+		}
+	}
+	return nil
+}
+
+func (f *WorkFile) SortBlocks() {
+	f.removeDups() // otherwise sorting is unsafe
+
+	for _, stmt := range f.Syntax.Stmt {
+		block, ok := stmt.(*LineBlock)
+		if !ok {
+			continue
+		}
+		sort.SliceStable(block.Line, func(i, j int) bool {
+			return lineLess(block.Line[i], block.Line[j])
+		})
+	}
+}
+
+// removeDups removes duplicate replace directives.
+//
+// Later replace directives take priority.
+//
+// require directives are not de-duplicated. That's left up to higher-level
+// logic (MVS).
+//
+// retract directives are not de-duplicated since comments are
+// meaningful, and versions may be retracted multiple times.
+func (f *WorkFile) removeDups() {
+	removeDups(f.Syntax, nil, &f.Replace)
+}
diff --git a/modfile/work_test.go b/modfile/work_test.go
new file mode 100644
index 0000000..90f0f55
--- /dev/null
+++ b/modfile/work_test.go
@@ -0,0 +1,402 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modfile
+
+import (
+	"bytes"
+	"io/ioutil"
+	"path/filepath"
+	"strings"
+	"testing"
+)
+
+// TODO(#45713): Update these tests once AddDirectory sets the module path.
+var workAddDirectoryTests = []struct {
+	desc       string
+	in         string
+	path       string
+	modulePath string
+	out        string
+}{
+	{
+		`empty`,
+		``,
+		`foo`, `bar`,
+		`directory foo`,
+	},
+	{
+		`go_stmt_only`,
+		`go 1.17
+		`,
+		`foo`, `bar`,
+		`go 1.17
+		directory foo
+		`,
+	},
+	{
+		`directory_line_present`,
+		`go 1.17
+		directory baz`,
+		`foo`, `bar`,
+		`go 1.17
+		directory (
+			baz
+		  foo
+		)
+		`,
+	},
+	{
+		`directory_block_present`,
+		`go 1.17
+		directory (
+			baz
+			quux
+		)
+		`,
+		`foo`, `bar`,
+		`go 1.17
+		directory (
+			baz
+		  quux
+			foo
+		)
+		`,
+	},
+	{
+		`directory_and_replace_present`,
+		`go 1.17
+		directory baz
+		replace a => ./b
+		`,
+		`foo`, `bar`,
+		`go 1.17
+		directory (
+			baz
+			foo
+		)
+		replace a => ./b
+		`,
+	},
+}
+
+var workDropDirectoryTests = []struct {
+	desc string
+	in   string
+	path string
+	out  string
+}{
+	{
+		`empty`,
+		``,
+		`foo`,
+		``,
+	},
+	{
+		`go_stmt_only`,
+		`go 1.17
+		`,
+		`foo`,
+		`go 1.17
+		`,
+	},
+	{
+		`singled_directory`,
+		`go 1.17
+		directory foo`,
+		`foo`,
+		`go 1.17
+		`,
+	},
+	{
+		`directory_block`,
+		`go 1.17
+		directory (
+			foo
+			bar
+			baz
+		)`,
+		`bar`,
+		`go 1.17
+		directory (
+			foo
+			baz
+		)`,
+	},
+	{
+		`directory_multi`,
+		`go 1.17
+		directory (
+			foo
+			bar
+			baz
+		)
+		directory foo
+		directory quux
+		directory foo`,
+		`foo`,
+		`go 1.17
+		directory (
+			bar
+			baz
+		)
+		directory quux`,
+	},
+}
+
+var workAddGoTests = []struct {
+	desc    string
+	in      string
+	version string
+	out     string
+}{
+	{
+		`empty`,
+		``,
+		`1.17`,
+		`go 1.17
+		`,
+	},
+	{
+		`comment`,
+		`// this is a comment`,
+		`1.17`,
+		`// this is a comment
+
+		go 1.17`,
+	},
+	{
+		`directory_after_replace`,
+		`
+		replace example.com/foo => ../bar
+		directory foo
+		`,
+		`1.17`,
+		`
+		go 1.17
+		replace example.com/foo => ../bar
+		directory foo
+		`,
+	},
+	{
+		`directory_before_replace`,
+		`directory foo
+		replace example.com/foo => ../bar
+		`,
+		`1.17`,
+		`
+		go 1.17
+		directory foo
+		replace example.com/foo => ../bar
+		`,
+	},
+	{
+		`directory_only`,
+		`directory foo
+		`,
+		`1.17`,
+		`
+		go 1.17
+		directory foo
+		`,
+	},
+	{
+		`already_have_go`,
+		`go 1.17
+		`,
+		`1.18`,
+		`
+		go 1.18
+		`,
+	},
+}
+
+var workSortBlocksTests = []struct {
+	desc, in, out string
+}{
+	{
+		`directory_duplicates_not_removed`,
+		`go 1.17
+		directory foo
+		directory bar
+		directory (
+			foo
+		)`,
+		`go 1.17
+		directory foo
+		directory bar
+		directory (
+			foo
+		)`,
+	},
+	{
+		`replace_duplicates_removed`,
+		`go 1.17
+		directory foo
+		replace x.y/z v1.0.0 => ./a
+		replace x.y/z v1.1.0 => ./b
+		replace (
+			x.y/z v1.0.0 => ./c
+		)
+		`,
+		`go 1.17
+		directory foo
+		replace x.y/z v1.1.0 => ./b
+		replace (
+			x.y/z v1.0.0 => ./c
+		)
+		`,
+	},
+}
+
+func TestAddDirectory(t *testing.T) {
+	for _, tt := range workAddDirectoryTests {
+		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)
+			})
+		})
+	}
+}
+
+func TestDropDirectory(t *testing.T) {
+	for _, tt := range workDropDirectoryTests {
+		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 {
+					return err
+				}
+				f.Cleanup()
+				return nil
+			})
+		})
+	}
+}
+
+func TestWorkAddGo(t *testing.T) {
+	for _, tt := range workAddGoTests {
+		t.Run(tt.desc, func(t *testing.T) {
+			testWorkEdit(t, tt.in, tt.out, func(f *WorkFile) error {
+				return f.AddGoStmt(tt.version)
+			})
+		})
+	}
+}
+
+func TestWorkSortBlocks(t *testing.T) {
+	for _, tt := range workSortBlocksTests {
+		t.Run(tt.desc, func(t *testing.T) {
+			testWorkEdit(t, tt.in, tt.out, func(f *WorkFile) error {
+				f.SortBlocks()
+				return nil
+			})
+		})
+	}
+}
+
+// Test that when files in the testdata directory are parsed
+// and printed and parsed again, we get the same parse tree
+// both times.
+func TestWorkPrintParse(t *testing.T) {
+	outs, err := filepath.Glob("testdata/work/*")
+	if err != nil {
+		t.Fatal(err)
+	}
+	for _, out := range outs {
+		out := out
+		name := filepath.Base(out)
+		t.Run(name, func(t *testing.T) {
+			t.Parallel()
+			data, err := ioutil.ReadFile(out)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			base := "testdata/work/" + filepath.Base(out)
+			f, err := parse(base, data)
+			if err != nil {
+				t.Fatalf("parsing original: %v", err)
+			}
+
+			ndata := Format(f)
+			f2, err := parse(base, ndata)
+			if err != nil {
+				t.Fatalf("parsing reformatted: %v", err)
+			}
+
+			eq := eqchecker{file: base}
+			if err := eq.check(f, f2); err != nil {
+				t.Errorf("not equal (parse/Format/parse): %v", err)
+			}
+
+			pf1, err := ParseWork(base, data, nil)
+			if err != nil {
+				switch base {
+				case "testdata/replace2.in", "testdata/gopkg.in.golden":
+					t.Errorf("should parse %v: %v", base, err)
+				}
+			}
+			if err == nil {
+				pf2, err := ParseWork(base, ndata, nil)
+				if err != nil {
+					t.Fatalf("Parsing reformatted: %v", err)
+				}
+				eq := eqchecker{file: base}
+				if err := eq.check(pf1, pf2); err != nil {
+					t.Errorf("not equal (parse/Format/Parse): %v", err)
+				}
+
+				ndata2 := Format(pf1.Syntax)
+				pf3, err := ParseWork(base, ndata2, nil)
+				if err != nil {
+					t.Fatalf("Parsing reformatted2: %v", err)
+				}
+				eq = eqchecker{file: base}
+				if err := eq.check(pf1, pf3); err != nil {
+					t.Errorf("not equal (Parse/Format/Parse): %v", err)
+				}
+				ndata = ndata2
+			}
+
+			if strings.HasSuffix(out, ".in") {
+				golden, err := ioutil.ReadFile(strings.TrimSuffix(out, ".in") + ".golden")
+				if err != nil {
+					t.Fatal(err)
+				}
+				if !bytes.Equal(ndata, golden) {
+					t.Errorf("formatted %s incorrectly: diff shows -golden, +ours", base)
+					tdiff(t, string(golden), string(ndata))
+					return
+				}
+			}
+		})
+	}
+}
+
+func testWorkEdit(t *testing.T, in, want string, transform func(f *WorkFile) error) *WorkFile {
+	t.Helper()
+	parse := ParseWork
+	f, err := parse("in", []byte(in), nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	g, err := parse("out", []byte(want), nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	golden := Format(g.Syntax)
+
+	if err := transform(f); err != nil {
+		t.Fatal(err)
+	}
+	out := Format(f.Syntax)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !bytes.Equal(out, golden) {
+		t.Errorf("have:\n%s\nwant:\n%s", out, golden)
+	}
+
+	return f
+}