github.com/ldez/motoko@v0.3.0/motoko_test.go (about)

     1  package main
     2  
     3  import (
     4  	"go/build"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func Test_updateCmd(t *testing.T) {
    11  	if os.Getenv("CI") == "true" && build.Default.GOOS == "windows" {
    12  		// Because the cleanup doesn't work well on Windows in GitHub Actions
    13  		// The process cannot access the file because it is being used by another process.
    14  		t.Skipf("Windows and GitHub Actions")
    15  	}
    16  
    17  	type expected struct {
    18  		code string
    19  		mod  string
    20  	}
    21  
    22  	testCases := []struct {
    23  		desc     string
    24  		version  string
    25  		expected expected
    26  	}{
    27  		{
    28  			desc:    "only number",
    29  			version: "20",
    30  			expected: expected{
    31  				code: sampleMain20,
    32  				mod:  sampleGoMod20,
    33  			},
    34  		},
    35  		{
    36  			desc:    "version prefixed by v",
    37  			version: "v20",
    38  			expected: expected{
    39  				code: sampleMain20,
    40  				mod:  sampleGoMod20,
    41  			},
    42  		},
    43  		{
    44  			desc:    "full version",
    45  			version: "v20.0.0",
    46  			expected: expected{
    47  				code: sampleMain20,
    48  				mod:  sampleGoMod20,
    49  			},
    50  		},
    51  	}
    52  
    53  	for _, test := range testCases {
    54  		test := test
    55  		t.Run(test.desc, func(t *testing.T) {
    56  			dir, err := setupTestProject(t)
    57  			if err != nil {
    58  				t.Fatal(err)
    59  			}
    60  
    61  			wd, err := os.Getwd()
    62  			if err != nil {
    63  				t.Fatal(err)
    64  			}
    65  			t.Cleanup(func() { _ = os.Chdir(wd) })
    66  
    67  			if os.Chdir(dir) != nil {
    68  				t.Fatal(err)
    69  			}
    70  
    71  			cfg := config{
    72  				lib:     "github.com/google/go-github",
    73  				version: test.version,
    74  			}
    75  			err = updateCmd(cfg)
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  
    80  			content, err := os.ReadFile(filepath.Join(filepath.Clean(dir), "main.go"))
    81  			if err != nil {
    82  				t.Fatal(err)
    83  			}
    84  
    85  			if string(content) != test.expected.code {
    86  				t.Errorf("got diffs:\n%s", quickDiff(string(content), test.expected.code))
    87  			}
    88  
    89  			mod, err := os.ReadFile(filepath.Join(filepath.Clean(dir), "go.mod"))
    90  			if err != nil {
    91  				t.Fatal(err)
    92  			}
    93  
    94  			if string(mod) != test.expected.mod {
    95  				t.Log(string(mod))
    96  				t.Errorf("got diffs:\n%s", quickDiff(string(mod), test.expected.mod))
    97  			}
    98  		})
    99  	}
   100  }