Skip to content

Commit e6f3871

Browse files
authored
test: add e2e test for clean and copy functions (#1849)
Fixes #1591
1 parent e7ae7ea commit e6f3871

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

generate_e2e_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,120 @@ func TestRunGenerate(t *testing.T) {
9797
}
9898
}
9999

100+
func TestCleanAndCopy(t *testing.T) {
101+
const (
102+
localAPISource = "testdata/e2e/generate/api_root"
103+
apiToGenerate = "google/cloud/pubsub/v1"
104+
)
105+
// create a temp directory for writing files, so we don't have to create testdata files.
106+
repoInitDir := t.TempDir()
107+
108+
// within the source root, create a file to be removed,
109+
// then create a sub dir with 2 files, on of them should be preserved.
110+
pubsubDir := filepath.Join(repoInitDir, "pubsub")
111+
if err := os.MkdirAll(filepath.Join(pubsubDir, "sub"), 0755); err != nil {
112+
t.Fatal(err)
113+
}
114+
if err := os.WriteFile(filepath.Join(pubsubDir, "file_to_remove.txt"), []byte("remove me"), 0644); err != nil {
115+
t.Fatal(err)
116+
}
117+
if err := os.WriteFile(filepath.Join(pubsubDir, "sub", "file_to_preserve.txt"), []byte("preserve me"), 0644); err != nil {
118+
t.Fatal(err)
119+
}
120+
if err := os.WriteFile(filepath.Join(pubsubDir, "sub", "another_file_to_remove.txt"), []byte("remove me"), 0644); err != nil {
121+
t.Fatal(err)
122+
}
123+
// Create a file outside the source root to ensure it's not touched.
124+
otherDir := filepath.Join(repoInitDir, "other_dir")
125+
if err := os.MkdirAll(otherDir, 0755); err != nil {
126+
t.Fatal(err)
127+
}
128+
if err := os.WriteFile(filepath.Join(otherDir, "file_to_keep.txt"), []byte("keep me"), 0644); err != nil {
129+
t.Fatal(err)
130+
}
131+
132+
// create a state file with remove and preserve regex.
133+
state := &config.LibrarianState{
134+
Image: "test-image:latest",
135+
Libraries: []*config.LibraryState{
136+
{
137+
ID: "go-google-cloud-pubsub-v1",
138+
Version: "v1.0.0",
139+
APIs: []*config.API{
140+
{
141+
Path: "google/cloud/pubsub/v1",
142+
},
143+
},
144+
SourceRoots: []string{"pubsub"},
145+
RemoveRegex: []string{
146+
"pubsub/file_to_remove.txt",
147+
"^pubsub/sub/.*.txt",
148+
},
149+
PreserveRegex: []string{
150+
"pubsub/sub/file_to_preserve.txt",
151+
},
152+
},
153+
},
154+
}
155+
stateBytes, err := yaml.Marshal(state)
156+
if err != nil {
157+
t.Fatal(err)
158+
}
159+
if err := os.MkdirAll(filepath.Join(repoInitDir, ".librarian"), 0755); err != nil {
160+
t.Fatal(err)
161+
}
162+
if err := os.WriteFile(filepath.Join(repoInitDir, ".librarian", "state.yaml"), stateBytes, 0644); err != nil {
163+
t.Fatal(err)
164+
}
165+
166+
workRoot := t.TempDir()
167+
repo := t.TempDir()
168+
APISourceRepo := t.TempDir()
169+
if err := initRepo(t, repo, repoInitDir); err != nil {
170+
t.Fatalf("languageRepo prepare test error = %v", err)
171+
}
172+
if err := initRepo(t, APISourceRepo, localAPISource); err != nil {
173+
t.Fatalf("APISouceRepo prepare test error = %v", err)
174+
}
175+
176+
cmd := exec.Command(
177+
"go",
178+
"run",
179+
"github.com/googleapis/librarian/cmd/librarian",
180+
"generate",
181+
fmt.Sprintf("--api=%s", apiToGenerate),
182+
fmt.Sprintf("--output=%s", workRoot),
183+
fmt.Sprintf("--repo=%s", repo),
184+
fmt.Sprintf("--api-source=%s", APISourceRepo),
185+
)
186+
cmd.Stderr = os.Stderr
187+
cmd.Stdout = os.Stdout
188+
if err := cmd.Run(); err != nil {
189+
t.Fatalf("librarian generate command error = %v", err)
190+
}
191+
192+
// Check that the file to remove is gone.
193+
if _, err := os.Stat(filepath.Join(repo, "pubsub", "file_to_remove.txt")); !os.IsNotExist(err) {
194+
t.Errorf("pubsub/file_to_remove.txt should have been removed")
195+
}
196+
// Check that the other file to remove is gone.
197+
if _, err := os.Stat(filepath.Join(repo, "pubsub", "sub", "another_file_to_remove.txt")); !os.IsNotExist(err) {
198+
t.Errorf("pubsub/sub/another_file_to_remove.txt should have been removed")
199+
}
200+
// Check that the file to preserve is still there.
201+
if _, err := os.Stat(filepath.Join(repo, "pubsub", "sub", "file_to_preserve.txt")); os.IsNotExist(err) {
202+
t.Errorf("pubsub/sub/file_to_preserve.txt should have been preserved")
203+
}
204+
// Check that the file outside the source root is still there.
205+
if _, err := os.Stat(filepath.Join(repo, "other_dir", "file_to_keep.txt")); os.IsNotExist(err) {
206+
t.Errorf("other_dir/file_to_keep.txt should have been preserved")
207+
}
208+
// check that the new files are copied. The fake generator creates a file called "example.txt".
209+
if _, err := os.Stat(filepath.Join(repo, "pubsub", "example.txt")); os.IsNotExist(err) {
210+
t.Errorf("pubsub/example.txt should have been copied")
211+
}
212+
}
213+
100214
func TestRunConfigure(t *testing.T) {
101215
const (
102216
localRepoDir = "testdata/e2e/configure/repo"

0 commit comments

Comments
 (0)