github.com/hashicorp/go-getter/v2@v2.2.2/get_hg_test.go (about) 1 package getter 2 3 import ( 4 "context" 5 "net/url" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "runtime" 10 "strings" 11 "testing" 12 "time" 13 14 testing_helper "github.com/hashicorp/go-getter/v2/helper/testing" 15 ) 16 17 var testHasHg bool 18 19 func init() { 20 if _, err := exec.LookPath("hg"); err == nil { 21 testHasHg = true 22 } 23 } 24 25 func TestHgGetter_impl(t *testing.T) { 26 var _ Getter = new(HgGetter) 27 } 28 29 func TestHgGetter(t *testing.T) { 30 if !testHasHg { 31 t.Log("hg not found, skipping") 32 t.Skip() 33 } 34 ctx := context.Background() 35 36 g := new(HgGetter) 37 dst := testing_helper.TempDir(t) 38 39 req := &Request{ 40 Dst: dst, 41 u: testModuleURL("basic-hg"), 42 } 43 44 // With a dir that doesn't exist 45 if err := g.Get(ctx, req); err != nil { 46 t.Fatalf("err: %s", err) 47 } 48 49 // Verify the main file exists 50 mainPath := filepath.Join(dst, "main.tf") 51 if _, err := os.Stat(mainPath); err != nil { 52 t.Fatalf("err: %s", err) 53 } 54 } 55 56 func TestHgGetter_branch(t *testing.T) { 57 if !testHasHg { 58 t.Log("hg not found, skipping") 59 t.Skip() 60 } 61 ctx := context.Background() 62 63 g := new(HgGetter) 64 dst := testing_helper.TempDir(t) 65 66 url := testModuleURL("basic-hg") 67 q := url.Query() 68 q.Add("rev", "test-branch") 69 url.RawQuery = q.Encode() 70 71 req := &Request{ 72 Dst: dst, 73 u: url, 74 } 75 76 if err := g.Get(ctx, req); err != nil { 77 t.Fatalf("err: %s", err) 78 } 79 80 // Verify the main file exists 81 mainPath := filepath.Join(dst, "main_branch.tf") 82 if _, err := os.Stat(mainPath); err != nil { 83 t.Fatalf("err: %s", err) 84 } 85 86 // Get again should work 87 if err := g.Get(ctx, req); err != nil { 88 t.Fatalf("err: %s", err) 89 } 90 91 // Verify the main file exists 92 mainPath = filepath.Join(dst, "main_branch.tf") 93 if _, err := os.Stat(mainPath); err != nil { 94 t.Fatalf("err: %s", err) 95 } 96 } 97 98 func TestHgGetter_GetFile(t *testing.T) { 99 if !testHasHg { 100 t.Log("hg not found, skipping") 101 t.Skip() 102 } 103 ctx := context.Background() 104 105 g := new(HgGetter) 106 dst := testing_helper.TempTestFile(t) 107 defer os.RemoveAll(filepath.Dir(dst)) 108 109 req := &Request{ 110 Dst: dst, 111 u: testModuleURL("basic-hg/foo.txt"), 112 } 113 114 // Download 115 if err := g.GetFile(ctx, req); err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 // Verify the main file exists 120 if _, err := os.Stat(dst); err != nil { 121 t.Fatalf("err: %s", err) 122 } 123 testing_helper.AssertContents(t, dst, "Hello\n") 124 } 125 func TestHgGetter_HgArgumentsNotAllowed(t *testing.T) { 126 if !testHasHg { 127 t.Log("hg not found, skipping") 128 t.Skip() 129 } 130 131 if runtime.GOOS == "windows" { 132 // Please refer to https://github.com/hashicorp/go-getter/pull/388/files#r1005819432 133 // for more context why we are temporarily skipping Windows OS. 134 t.Log("skipping on Windows OS for now") 135 t.Skip() 136 } 137 ctx := context.Background() 138 139 tc := []struct { 140 name string 141 req Request 142 errChk func(testing.TB, error) 143 }{ 144 { 145 // If arguments are allowed in the destination, this request to Get will fail 146 name: "arguments allowed in destination", 147 req: Request{ 148 Dst: "--config=alias.clone=!touch ./TEST", 149 u: testModuleURL("basic-hg"), 150 }, 151 errChk: func(t testing.TB, err error) { 152 if err != nil { 153 t.Errorf("Expected no err, got: %s", err) 154 } 155 }, 156 }, 157 { 158 // Test arguments passed into the `rev` parameter 159 // This clone call will fail regardless, but an exit code of 1 indicates 160 // that the `false` command executed 161 // We are expecting an hg parse error 162 name: "arguments passed into rev parameter", 163 req: Request{ 164 u: testModuleURL("basic-hg?rev=--config=alias.update=!false"), 165 }, 166 errChk: func(t testing.TB, err error) { 167 if err == nil { 168 return 169 } 170 171 if !strings.Contains(err.Error(), "hg: parse error") { 172 t.Errorf("Expected no err, got: %s", err) 173 } 174 }, 175 }, 176 { 177 // Test arguments passed in the repository URL 178 // This Get call will fail regardless, but it should fail 179 // because the repository can't be found. 180 // Other failures indicate that hg interpreted the argument passed in the URL 181 name: "arguments passed in the repository URL", 182 req: Request{ 183 u: &url.URL{Path: "--config=alias.clone=false"}}, 184 errChk: func(t testing.TB, err error) { 185 if err == nil { 186 return 187 } 188 189 if !strings.Contains(err.Error(), "repository --config=alias.clone=false not found") { 190 t.Errorf("Expected no err, got: %s", err) 191 } 192 }, 193 }, 194 } 195 for _, tt := range tc { 196 tt := tt 197 t.Run(tt.name, func(t *testing.T) { 198 g := new(HgGetter) 199 200 if tt.req.Dst == "" { 201 dst := testing_helper.TempDir(t) 202 tt.req.Dst = dst 203 } 204 205 defer os.RemoveAll(tt.req.Dst) 206 err := g.Get(ctx, &tt.req) 207 tt.errChk(t, err) 208 }) 209 } 210 } 211 212 func TestHgGetter_GetWithTimeout(t *testing.T) { 213 if !testHasHg { 214 t.Log("hg not found, skipping") 215 t.Skip() 216 } 217 ctx := context.Background() 218 g := &HgGetter{ 219 Timeout: 1 * time.Millisecond, 220 } 221 222 dst := testing_helper.TempDir(t) 223 defer os.RemoveAll(filepath.Dir(dst)) 224 req := &Request{ 225 Dst: dst, 226 u: testModuleURL("basic-hg/foo.txt"), 227 } 228 229 if err := g.Get(ctx, req); err == nil { 230 t.Fatalf("err: %s", err.Error()) 231 } 232 }