github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/gen/merge_test.go (about)

     1  package gen
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"regexp"
     7  	"testing"
     8  )
     9  
    10  func TestMerge(t *testing.T) {
    11  
    12  	debug := true
    13  
    14  	type tcase struct {
    15  		name    string
    16  		infiles map[string]string   // file structure to start with
    17  		out     map[string][]string // regexps to match in output files
    18  		outNot  map[string][]string // regexps to NOT match in output files
    19  	}
    20  
    21  	tcList := []tcase{
    22  		{
    23  			name: "simple",
    24  			infiles: map[string]string{
    25  				"file1.go": "package main\nfunc main(){}",
    26  				"file2.go": "package main\nvar a string",
    27  			},
    28  			out: map[string][]string{
    29  				"out.go": {`func main`, `var a string`},
    30  			},
    31  		},
    32  		{
    33  			name: "comments",
    34  			infiles: map[string]string{
    35  				"file1.go": "package main\n// main comment here\nfunc main(){}",
    36  				"file2.go": "package main\nvar a string // a comment here\n",
    37  			},
    38  			out: map[string][]string{
    39  				"out.go": {`func main`, `// main comment here`, `var a string`, `// a comment here`},
    40  			},
    41  		},
    42  		{
    43  			name: "import-dedup",
    44  			infiles: map[string]string{
    45  				"file1.go": "package main\nimport \"fmt\"\n// main comment here\nfunc main(){}",
    46  				"file2.go": "package main\nimport \"fmt\"\nvar a string // a comment here\n",
    47  			},
    48  			out: map[string][]string{
    49  				"out.go": {`import "fmt"`},
    50  			},
    51  			outNot: map[string][]string{
    52  				"out.go": {`(?ms)import "fmt".*import "fmt"`},
    53  			},
    54  		},
    55  		{
    56  			name: "import-dedup-2",
    57  			infiles: map[string]string{
    58  				"file1.go": "package main\nimport \"fmt\"\n// main comment here\nfunc main(){}",
    59  				"file2.go": "package main\nimport \"fmt\"\nimport \"log\"\nvar a string // a comment here\n",
    60  			},
    61  			out: map[string][]string{
    62  				"out.go": {`import "fmt"`, `import "log"`},
    63  			},
    64  			outNot: map[string][]string{
    65  				"out.go": {`(?ms)\}.*import "log"`},
    66  			},
    67  		},
    68  	}
    69  
    70  	for _, tc := range tcList {
    71  		tc := tc
    72  		t.Run(tc.name, func(t *testing.T) {
    73  
    74  			tmpDir, err := os.MkdirTemp("", "TestMerge")
    75  			if err != nil {
    76  				t.Fatal(err)
    77  			}
    78  
    79  			if debug {
    80  				t.Logf("Test %q using tmpDir: %s", tc.name, tmpDir)
    81  			} else {
    82  				defer os.RemoveAll(tmpDir)
    83  				t.Parallel()
    84  			}
    85  
    86  			tstWriteFiles(tmpDir, tc.infiles)
    87  			var in []string
    88  			for k := range tc.infiles {
    89  				// in = append(in, filepath.Join(tmpDir, k))
    90  				in = append(in, k)
    91  			}
    92  
    93  			err = mergeGoFiles(tmpDir, "out.go", in...)
    94  			if err != nil {
    95  				t.Fatal(err)
    96  			}
    97  
    98  			for fname, patterns := range tc.out {
    99  				b, err := os.ReadFile(filepath.Join(tmpDir, fname))
   100  				if err != nil {
   101  					t.Errorf("failed to read file %q after Run: %v", fname, err)
   102  					continue
   103  				}
   104  				for _, pattern := range patterns {
   105  					re := regexp.MustCompile(pattern)
   106  					if !re.Match(b) {
   107  						t.Errorf("failed to match regexp on file %q: %s", fname, pattern)
   108  					}
   109  				}
   110  			}
   111  
   112  			for fname, patterns := range tc.outNot {
   113  				b, err := os.ReadFile(filepath.Join(tmpDir, fname))
   114  				if err != nil {
   115  					t.Errorf("failed to read file %q after Run: %v", fname, err)
   116  					continue
   117  				}
   118  				for _, pattern := range patterns {
   119  					re := regexp.MustCompile(pattern)
   120  					if re.Match(b) {
   121  						t.Errorf("incorrectly matched regexp on file %q: %s", fname, pattern)
   122  					}
   123  				}
   124  			}
   125  
   126  			if debug {
   127  				outb, _ := os.ReadFile(filepath.Join(tmpDir, "out.go"))
   128  				t.Logf("OUTPUT:\n%s", outb)
   129  			}
   130  
   131  		})
   132  	}
   133  
   134  }