github.com/asdine/cueimports@v0.3.2/cueimports_test.go (about)

     1  package cueimports
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"cuelang.org/go/cue/parser"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestImport(t *testing.T) {
    13  	err := os.Chdir("testdata")
    14  	require.NoError(t, err)
    15  
    16  	tests := []struct {
    17  		filename    string
    18  		passContent bool
    19  		want        string
    20  	}{
    21  		{"file.cue", false, "file.cue.golden"},
    22  		{"file.cue", true, "file.cue.golden"},
    23  		{"default.cue", false, "default.cue.golden"},
    24  		{"default.cue", true, "default.cue.golden"},
    25  		{"unchanged.cue", false, "unchanged.cue.golden"},
    26  		{"unchanged.cue", true, "unchanged.cue.golden"},
    27  		{"unused_import.cue", false, "unused_import.cue.golden"},
    28  		{"unused_import.cue", true, "unused_import.cue.golden"},
    29  		{"top_level_name_clash.cue", false, "top_level_name_clash.cue.golden"},
    30  		{"top_level_name_clash.cue", true, "top_level_name_clash.cue.golden"},
    31  		{"import_colon.cue", true, "import_colon.cue.golden"},
    32  		{"import_colon_unchanged.cue", true, "import_colon_unchanged.cue.golden"},
    33  	}
    34  
    35  	for _, tt := range tests {
    36  		t.Run(fmt.Sprintf("%s/%v", tt.filename, tt.passContent), func(t *testing.T) {
    37  			var content []byte
    38  			if tt.passContent {
    39  				content, err = os.ReadFile(tt.filename)
    40  				require.NoError(t, err)
    41  			}
    42  			res, err := Import(tt.filename, content)
    43  			require.NoError(t, err)
    44  
    45  			expected, err := os.ReadFile(tt.want)
    46  			require.NoError(t, err)
    47  
    48  			require.Equal(t, string(expected), string(res))
    49  		})
    50  	}
    51  }
    52  
    53  func TestImportInsertImports(t *testing.T) {
    54  	tests := []struct {
    55  		name     string
    56  		input    string
    57  		packages map[string]string
    58  		want     string
    59  	}{
    60  		{
    61  			name:  "empty file",
    62  			input: "",
    63  			want:  "\n",
    64  		},
    65  		{
    66  			name:  "no package",
    67  			input: "b: math.Round(1.5)",
    68  			packages: map[string]string{
    69  				"math": "math",
    70  			},
    71  			want: `import "math"
    72  
    73  b: math.Round(1.5)
    74  `,
    75  		},
    76  		{
    77  			name: "with package",
    78  			input: `package test
    79  
    80  			b: math.Round(1.5)
    81  `,
    82  			packages: map[string]string{
    83  				"math": "math",
    84  			},
    85  			want: `package test
    86  
    87  import "math"
    88  
    89  b: math.Round(1.5)
    90  `,
    91  		},
    92  		{
    93  			name: "with comment",
    94  			input: `package test
    95  			// some comment
    96  
    97  			b: math.Round(1.5)
    98  `,
    99  			packages: map[string]string{
   100  				"math": "math",
   101  			},
   102  			want: `package test
   103  
   104  // some comment
   105  import "math"
   106  
   107  b: math.Round(1.5)
   108  `,
   109  		},
   110  		{
   111  			name: "with comment at the top",
   112  			input: `// some comment
   113  			package test
   114  
   115  			b: math.Round(1.5)
   116  `,
   117  			packages: map[string]string{
   118  				"math": "math",
   119  			},
   120  			want: `// some comment
   121  package test
   122  
   123  import "math"
   124  
   125  b: math.Round(1.5)
   126  `,
   127  		},
   128  		{
   129  			name: "with existing import",
   130  			input: `import "math"
   131  
   132  b: math.Round(1.5)
   133  `,
   134  			packages: map[string]string{
   135  				"math": "math",
   136  			},
   137  			want: `import "math"
   138  
   139  b: math.Round(1.5)
   140  `,
   141  		},
   142  		{
   143  			name: "with existing different import",
   144  			input: `import "encoding/json"
   145  
   146  b: math.Round(1.5)
   147  c: json.Marshal(1)
   148  `,
   149  			packages: map[string]string{
   150  				"math": "math",
   151  				"json": "encoding/json",
   152  			},
   153  			want: `import (
   154  	"encoding/json"
   155  	"math"
   156  )
   157  
   158  b: math.Round(1.5)
   159  c: json.Marshal(1)
   160  `,
   161  		},
   162  		{
   163  			name: "with custom package",
   164  			input: `
   165  a: foo.#Bar
   166  b: math.Round(1.5)
   167  c: json.Marshal(1)
   168  `,
   169  			packages: map[string]string{
   170  				"math": "math",
   171  				"json": "encoding/json",
   172  				"foo":  "test.com/foo",
   173  			},
   174  			want: `import (
   175  	"encoding/json"
   176  	"math"
   177  	
   178  	"test.com/foo"
   179  )
   180  
   181  a: foo.#Bar
   182  b: math.Round(1.5)
   183  c: json.Marshal(1)
   184  `,
   185  		},
   186  		{
   187  			name: "with std package clash",
   188  			input: `import "encoding/json"
   189  a: tool.#Foo
   190  b: math.Round(1.5)
   191  c: json.Marshal(1)
   192  `,
   193  			packages: map[string]string{
   194  				"math": "math",
   195  				"json": "encoding/json",
   196  				"tool": "foo.com/tool",
   197  			},
   198  			want: `import (
   199  	"encoding/json"
   200  	"math"
   201  	
   202  	"foo.com/tool"
   203  )
   204  
   205  a: tool.#Foo
   206  b: math.Round(1.5)
   207  c: json.Marshal(1)
   208  `,
   209  		},
   210  	}
   211  
   212  	for _, tt := range tests {
   213  		t.Run(tt.name, func(t *testing.T) {
   214  			f, err := parser.ParseFile("", tt.input,
   215  				parser.ParseComments,
   216  				parser.AllowPartial,
   217  			)
   218  			require.NoError(t, err)
   219  			res, err := insertImports(f, tt.packages)
   220  			require.NoError(t, err)
   221  			require.Equal(t, tt.want, string(res))
   222  		})
   223  	}
   224  }