github.com/cornelk/go-cloud@v0.17.1/internal/releasehelper/releasehelper_test.go (about) 1 // Copyright 2019 The Go Cloud Development Kit 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 // https://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 main 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 ) 25 26 // The following directory/file structure is created. ROOT is the root temp 27 // directory created by the test. 28 // 29 // ROOT/go.mod <-- main go.mod for github.com/cornelk/go-cloud 30 // ROOT/submod/go.mod <-- go.mod for a submodule of github.com/cornelk/go-cloud 31 // ROOT/samples/go.mod <-- go.mod for "samples" that include both of the 32 // other modules 33 var mainGomod = []byte("module github.com/cornelk/go-cloud\n") 34 35 var submodGomod = []byte(`module github.com/cornelk/go-cloud/submod 36 37 require ( 38 github.com/cornelk/go-cloud v0.15.0 39 ) 40 `) 41 42 var samplesGomod = []byte(`module github.com/cornelk/go-cloud/samples 43 44 require ( 45 github.com/cornelk/go-cloud v0.15.0 46 github.com/cornelk/go-cloud/submod v0.15.0 47 ) 48 `) 49 50 func createFilesForTest(root string) error { 51 if err := ioutil.WriteFile(filepath.Join(root, "go.mod"), mainGomod, 0666); err != nil { 52 return err 53 } 54 if err := os.MkdirAll(filepath.Join(root, "submod"), 0766); err != nil { 55 return err 56 } 57 if err := ioutil.WriteFile(filepath.Join(root, "submod", "go.mod"), submodGomod, 0666); err != nil { 58 return err 59 } 60 if err := os.MkdirAll(filepath.Join(root, "samples"), 0766); err != nil { 61 return err 62 } 63 if err := ioutil.WriteFile(filepath.Join(root, "samples", "go.mod"), samplesGomod, 0666); err != nil { 64 return err 65 } 66 return nil 67 } 68 69 func Test(t *testing.T) { 70 tempDir, err := ioutil.TempDir("", "releasehelper_test") 71 if err != nil { 72 t.Fatal(err) 73 } 74 fmt.Println("temp dir:", tempDir) 75 if err := createFilesForTest(tempDir); err != nil { 76 t.Fatal(err) 77 } 78 defer os.RemoveAll(tempDir) 79 80 if err := os.Chdir(tempDir); err != nil { 81 t.Fatal(err) 82 } 83 84 // Add replace lines and expect to find them. 85 gomodAddReplace("samples") 86 87 samplesGomod := filepath.Join("samples", "go.mod") 88 c, err := ioutil.ReadFile(samplesGomod) 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 replaceLines := []string{ 94 "replace github.com/cornelk/go-cloud => " + filepath.Clean("../"), 95 "replace github.com/cornelk/go-cloud/submod => " + filepath.Clean("../submod")} 96 97 for _, line := range replaceLines { 98 if !strings.Contains(string(c), line) { 99 t.Errorf("Expected to find '%s' in samples/go.mod", line) 100 } 101 } 102 103 // Drop replace lines and expect not to find them. 104 gomodDropReplace("samples") 105 c, err = ioutil.ReadFile(samplesGomod) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 for _, line := range replaceLines { 111 if strings.Contains(string(c), line) { 112 t.Errorf("Expected to not find '%s' in samples/go.mod", line) 113 } 114 } 115 116 // Set new version and check it was set as expected. 117 gomodSetVersion("samples", "v1.8.99") 118 c, err = ioutil.ReadFile(samplesGomod) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 if !strings.Contains(string(c), "github.com/cornelk/go-cloud v1.8.99") || !strings.Contains(string(c), "github.com/cornelk/go-cloud/submod v1.8.99") { 124 t.Error("New versions for require not found in samples/go.mod") 125 } 126 }