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