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