cuelang.org/go@v0.10.1/internal/mod/modimports/modimports_test.go (about)

     1  package modimports
     2  
     3  import (
     4  	"fmt"
     5  	"io/fs"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/go-quicktest/qt"
    11  	"github.com/google/go-cmp/cmp"
    12  	"golang.org/x/tools/txtar"
    13  )
    14  
    15  func TestAllPackageFiles(t *testing.T) {
    16  	files, err := filepath.Glob("testdata/*.txtar")
    17  	qt.Assert(t, qt.IsNil(err))
    18  	for _, f := range files {
    19  		t.Run(f, func(t *testing.T) {
    20  			ar, err := txtar.ParseFile(f)
    21  			qt.Assert(t, qt.IsNil(err))
    22  			tfs, err := txtar.FS(ar)
    23  			qt.Assert(t, qt.IsNil(err))
    24  			want, err := fs.ReadFile(tfs, "want")
    25  			qt.Assert(t, qt.IsNil(err))
    26  			iter := AllModuleFiles(tfs, ".")
    27  			var out strings.Builder
    28  			iter(func(pf ModuleFile, err error) bool {
    29  				out.WriteString(pf.FilePath)
    30  				if err != nil {
    31  					fmt.Fprintf(&out, ": error: %v\n", err)
    32  					return true
    33  				}
    34  				for _, imp := range pf.Syntax.Imports {
    35  					fmt.Fprintf(&out, " %s", imp.Path.Value)
    36  				}
    37  				out.WriteString("\n")
    38  				return true
    39  			})
    40  			if diff := cmp.Diff(string(want), out.String()); diff != "" {
    41  				t.Fatalf("unexpected results (-want +got):\n%s", diff)
    42  			}
    43  			wantImports, err := fs.ReadFile(tfs, "want-imports")
    44  			qt.Assert(t, qt.IsNil(err))
    45  			out.Reset()
    46  			imports, err := AllImports(AllModuleFiles(tfs, "."))
    47  			if err != nil {
    48  				fmt.Fprintf(&out, "error: %v\n", err)
    49  			} else {
    50  				for _, imp := range imports {
    51  					fmt.Fprintln(&out, imp)
    52  				}
    53  			}
    54  			if diff := cmp.Diff(string(wantImports), out.String()); diff != "" {
    55  				t.Fatalf("unexpected results for ImportsForModuleFiles (-want +got):\n%s", diff)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestPackageFiles(t *testing.T) {
    62  	dirContents := txtar.Parse([]byte(`
    63  -- x1.cue --
    64  package x
    65  
    66  import (
    67  	"foo"
    68  	"bar.com/baz"
    69  )
    70  -- x2.cue --
    71  package x
    72  
    73  import (
    74  	"something.else:other"
    75  	"bar.com/baz:baz"
    76  )
    77  -- y.cue --
    78  package y
    79  
    80  import (
    81  	"something.that.is/imported/by/y"
    82  )
    83  -- nopkg.cue --
    84  import (
    85  	"something.that.is/imported/by/nopkg"
    86  )
    87  -- foo/y.cue --
    88  import "other"
    89  -- omitted.go --
    90  -- omitted --
    91  -- sub/x.cue --
    92  package x
    93  import (
    94  	"imported-from-sub.com/foo"
    95  )
    96  `))
    97  	tfs, err := txtar.FS(dirContents)
    98  	qt.Assert(t, qt.IsNil(err))
    99  	imps, err := AllImports(PackageFiles(tfs, ".", "*"))
   100  	qt.Assert(t, qt.IsNil(err))
   101  	qt.Assert(t, qt.DeepEquals(imps, []string{
   102  		"bar.com/baz",
   103  		"foo",
   104  		"something.else:other",
   105  		"something.that.is/imported/by/nopkg",
   106  		"something.that.is/imported/by/y",
   107  	}))
   108  	imps, err = AllImports(PackageFiles(tfs, ".", "x"))
   109  	qt.Assert(t, qt.IsNil(err))
   110  	qt.Assert(t, qt.DeepEquals(imps, []string{
   111  		"bar.com/baz",
   112  		"foo",
   113  		"something.else:other",
   114  	}))
   115  	imps, err = AllImports(PackageFiles(tfs, "foo", "*"))
   116  	qt.Assert(t, qt.IsNil(err))
   117  	qt.Assert(t, qt.DeepEquals(imps, []string{"other"}))
   118  	imps, err = AllImports(PackageFiles(tfs, "sub", "x"))
   119  	qt.Assert(t, qt.IsNil(err))
   120  	qt.Assert(t, qt.DeepEquals(imps, []string{"bar.com/baz", "foo", "imported-from-sub.com/foo", "something.else:other"}))
   121  }