diff --git a/internal/imports/imports_test.go b/internal/imports/imports_test.go new file mode 100644 index 0000000000..7ab09b7d29 --- /dev/null +++ b/internal/imports/imports_test.go @@ -0,0 +1,74 @@ +package imports + +import ( + "go/build" + "testing" +) + +// TestNilOpts tests that process does not crash with nil opts. +func TestNilOpts(t *testing.T) { + var testOpts = []struct { + name string + opt *Options + }{ + { + name: "nil", + opt: nil, + }, + { + name: "nil env", + opt: &Options{Comments: true, TabIndent: true, TabWidth: 8}, + }, + { + name: "default", + opt: &Options{ + Env: &ProcessEnv{ + GOPATH: build.Default.GOPATH, + GOROOT: build.Default.GOROOT, + }, + Comments: true, + TabIndent: true, + TabWidth: 8, + }, + }, + } + + input := `package p + +func _() { + fmt.Println() +} +` + want := `package p + +import "fmt" + +func _() { + fmt.Println() +} +` + for _, test := range testOpts { + // Test Process + got, err := Process("", []byte(input), test.opt) + if err != nil { + t.Errorf("%s: %s", test.name, err.Error()) + } + if string(got) != want { + t.Errorf("%s: Process: Got:\n%s\nWant:\n%s\n", test.name, string(got), want) + } + + // Test FixImports and ApplyFixes + fixes, err := FixImports("", []byte(input), test.opt) + if err != nil { + t.Errorf("%s: %s", test.name, err.Error()) + } + + got, err = ApplyFixes(fixes, "", []byte(input), test.opt) + if err != nil { + t.Errorf("%s: %s", test.name, err.Error()) + } + if string(got) != want { + t.Errorf("%s: ApplyFix: Got:\n%s\nWant:\n%s\n", test.name, string(got), want) + } + } +}