cuelang.org/go@v0.13.0/internal/mod/modload/tidy_test.go (about)

     1  package modload
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/fs"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"cuelabs.dev/go/oci/ociregistry/ociclient"
    12  	"github.com/go-quicktest/qt"
    13  	"github.com/google/go-cmp/cmp"
    14  	"golang.org/x/tools/txtar"
    15  
    16  	"cuelang.org/go/mod/modcache"
    17  	"cuelang.org/go/mod/modregistry"
    18  	"cuelang.org/go/mod/modregistrytest"
    19  )
    20  
    21  func TestTidy(t *testing.T) {
    22  	files, err := filepath.Glob("testdata/tidy/*.txtar")
    23  	qt.Assert(t, qt.IsNil(err))
    24  	for _, f := range files {
    25  		t.Run(f, func(t *testing.T) {
    26  			ar, err := txtar.ParseFile(f)
    27  			qt.Assert(t, qt.IsNil(err))
    28  			tfs, err := txtar.FS(ar)
    29  			qt.Assert(t, qt.IsNil(err))
    30  			reg, _ := newRegistry(t, tfs)
    31  
    32  			want, err := fs.ReadFile(tfs, "want")
    33  			qt.Assert(t, qt.IsNil(err))
    34  
    35  			err = CheckTidy(context.Background(), tfs, ".", reg)
    36  			wantCheckTidyError := stringFromFile(tfs, "tidy-check-error")
    37  			if wantCheckTidyError == "" {
    38  				qt.Check(t, qt.IsNil(err))
    39  			} else {
    40  				qt.Check(t, qt.ErrorMatches(err, wantCheckTidyError))
    41  			}
    42  
    43  			var out strings.Builder
    44  			var tidyFile []byte
    45  			mf, err := Tidy(context.Background(), tfs, ".", reg)
    46  			if err != nil {
    47  				fmt.Fprintf(&out, "error: %v\n", err)
    48  			} else {
    49  				tidyFile, err = mf.Format()
    50  				qt.Assert(t, qt.IsNil(err))
    51  				out.Write(tidyFile)
    52  			}
    53  			if diff := cmp.Diff(string(want), out.String()); diff != "" {
    54  				t.Log("actual result:\n", out.String())
    55  				t.Fatalf("unexpected results (-want +got):\n%s", diff)
    56  			}
    57  
    58  			// Ensure that CheckTidy does not error after a successful Tidy.
    59  			// We make a new txtar FS given that an FS is read-only.
    60  			if len(tidyFile) > 0 {
    61  				for i := range ar.Files {
    62  					file := &ar.Files[i]
    63  					if file.Name == "cue.mod/module.cue" {
    64  						file.Data = []byte(out.String())
    65  					}
    66  				}
    67  				tfs, err := txtar.FS(ar)
    68  				qt.Assert(t, qt.IsNil(err))
    69  				err = CheckTidy(context.Background(), tfs, ".", reg)
    70  				qt.Check(t, qt.IsNil(err), qt.Commentf("CheckTidy after a successful Tidy should not fail"))
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func stringFromFile(fsys fs.FS, file string) string {
    77  	data, _ := fs.ReadFile(fsys, file)
    78  	return strings.TrimSpace(string(data))
    79  }
    80  
    81  func newRegistry(t *testing.T, fsys fs.FS) (Registry, string) {
    82  	fsys, err := fs.Sub(fsys, "_registry")
    83  	qt.Assert(t, qt.IsNil(err))
    84  	regSrv, err := modregistrytest.New(fsys, "")
    85  	qt.Assert(t, qt.IsNil(err))
    86  	t.Cleanup(regSrv.Close)
    87  	regOCI, err := ociclient.New(regSrv.Host(), &ociclient.Options{
    88  		Insecure: true,
    89  	})
    90  	qt.Assert(t, qt.IsNil(err))
    91  	cacheDir := t.TempDir()
    92  	reg, err := modcache.New(modregistry.NewClient(regOCI), cacheDir)
    93  	qt.Assert(t, qt.IsNil(err))
    94  	t.Cleanup(func() {
    95  		modcache.RemoveAll(cacheDir)
    96  	})
    97  	return reg, cacheDir
    98  }