gitlab.com/polyapp-open-source/poly@v0.0.0-20200304172929-90b164ae7520/cmd/common_test.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "runtime" 8 "strings" 9 "testing" 10 ) 11 12 // ExampleGetTargets is used in lieu of measuring stdout 13 func ExampleGetTargets() { 14 err := os.Chdir("../testdata") 15 if err != nil { 16 fmt.Println("Error changing to testdata directory: " + err.Error()) 17 } 18 defaultPaths, JSWASMPaths := GetTargets() 19 fmt.Println(defaultPaths) 20 fmt.Println(JSWASMPaths) 21 //nolint 22 // Output: 23 // [gitlab.com/polyapp-open-source/poly/testdata/cmd gitlab.com/polyapp-open-source/poly/testdata/cmd/hello] 24 // [gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm1 gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm2 gitlab.com/polyapp-open-source/poly/testdata/cmd/wasm3] 25 } 26 27 func TestGetOutputFileName(t *testing.T) { 28 fn := GetOutputFileName("/tmp/something", notWASM) 29 if runtime.GOOS == "windows" && fn != "something.exe" { 30 t.Error("expected file to be named 'something.exe' and got: " + fn) 31 } else if runtime.GOOS != "windows" && fn != "something" { 32 t.Error("expected file to be named 'something' and got: " + fn) 33 } 34 35 expectedFileName := "somethingelse_tinygo.wasm" 36 fn = GetOutputFileName("./tmp/somethingelse", tinygoWASM) 37 if runtime.GOOS == "windows" && fn != expectedFileName { 38 t.Error("expected file to be named '" + expectedFileName + "' and got: " + fn) 39 } else if runtime.GOOS != "windows" && fn != expectedFileName { 40 t.Error("expected file to be named '" + expectedFileName + "' and got: " + fn) 41 } 42 43 expectedFileName = "somethingelse.wasm" 44 fn = GetOutputFileName("./somethingelse", regularGoWASM) 45 if runtime.GOOS == "windows" && fn != expectedFileName { 46 t.Error("expected file to be named '" + expectedFileName + "' and got: " + fn) 47 } else if runtime.GOOS != "windows" && fn != expectedFileName { 48 t.Error("expected file to be named '" + expectedFileName + "' and got: " + fn) 49 } 50 } 51 52 func TestGetTargetsFromListOutput(t *testing.T) { 53 var list bytes.Buffer 54 list.WriteString(`gitlab.com/polyapp-open-source/poly/testdata/cmd 55 gitlab.com/polyapp-open-source/poly/testdata/cmd/hello 56 57 `) 58 targets := GetTargetsFromListOutput(list) 59 if len(targets) != 2 { 60 t.Error("Expected 2 targets based on the output of go list") 61 } 62 if targets[0] != "gitlab.com/polyapp-open-source/poly/testdata/cmd" { 63 t.Error("Didn't contain the expected first path") 64 } 65 if targets[1] != "gitlab.com/polyapp-open-source/poly/testdata/cmd/hello" { 66 t.Error("Didn't contain the expected second path") 67 } 68 } 69 70 func TestListPathToAbsPath(t *testing.T) { 71 in := "gitlab.com/polyapp-open-source/poly/testdata/cmd" 72 desiredOutSubstring := `src/gitlab.com/polyapp-open-source/poly/testdata/cmd` 73 actualOut := ListPathToAbsPath(in) 74 if !strings.Contains(actualOut, desiredOutSubstring) { 75 t.Errorf("in: %s \nout: %s \ndesired substring: %s\n", in, actualOut, desiredOutSubstring) 76 } 77 }