cuelang.org/go@v0.13.0/tools/trim/trim_test.go (about)

     1  // Copyright 2019 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package trim_test
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"cuelang.org/go/cue/build"
    22  	"cuelang.org/go/cue/errors"
    23  	"cuelang.org/go/internal/cuetdtest"
    24  	"cuelang.org/go/internal/cuetxtar"
    25  	"cuelang.org/go/tools/trim"
    26  	"github.com/go-quicktest/qt"
    27  )
    28  
    29  var (
    30  	matrix = cuetdtest.FullMatrix
    31  )
    32  
    33  const trace = false
    34  
    35  func TestTrimFiles(t *testing.T) {
    36  	test := cuetxtar.TxTarTest{
    37  		Root:   "./testdata",
    38  		Name:   "trim",
    39  		Matrix: matrix,
    40  	}
    41  
    42  	test.Run(t, func(t *cuetxtar.Test) {
    43  		a := t.Instance()
    44  		ctx := t.CueContext()
    45  		val := ctx.BuildInstance(a)
    46  		// Note: don't require val.Err to be nil because there are deliberate
    47  		// errors in some tests, to ensure trim still works even with some errors.
    48  		hadError := val.Err() != nil
    49  
    50  		files := a.Files
    51  
    52  		cfg := &trim.Config{
    53  			Trace: trace && testing.Verbose(),
    54  		}
    55  		var buf *bytes.Buffer
    56  		if cfg.Trace {
    57  			buf = new(bytes.Buffer)
    58  			cfg.TraceWriter = buf
    59  			// Uncomment if you find a test is panicking and still want
    60  			// tracing.
    61  			// cfg.TraceWriter = os.Stderr
    62  		}
    63  
    64  		err := trim.Files(files, val, cfg)
    65  		if buf != nil && buf.Len() != 0 {
    66  			t.Log("Trace:\n" + buf.String())
    67  		}
    68  		if err != nil {
    69  			t.WriteErrors(errors.Promote(err, ""))
    70  		}
    71  
    72  		// If the files could be built without an error before,
    73  		// they should still build without an error after trimming.
    74  		// This might not be true if, for example, unused imports are not removed.
    75  		// Note that we need a new build.Instance to build the ast.Files from scratch again.
    76  		if !hadError {
    77  			a := build.NewContext().NewInstance("", nil)
    78  			for _, file := range files {
    79  				a.AddSyntax(file)
    80  			}
    81  			val := ctx.BuildInstance(a)
    82  			qt.Assert(t, qt.IsNil(val.Err()))
    83  		}
    84  
    85  		for _, f := range files {
    86  			t.WriteFile(f)
    87  		}
    88  	})
    89  }