Skip to content

Commit 4bcdca8

Browse files
authored
feat(internal/librarian): removes library.output when tidy if can be derived (#3218)
When running `tidy`, remove `output` from library if can be derived from default. At generation, output is filled with default if empty [here](https://github.com/googleapis/librarian/blob/e36a73fdd000edab456b5ed9cbc70623b80596d7/internal/librarian/generate.go#L212). For #3139
1 parent e36a73f commit 4bcdca8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

internal/librarian/tidy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@ func RunTidy() error {
5151
if err := validateLibraries(cfg); err != nil {
5252
return err
5353
}
54+
for _, lib := range cfg.Libraries {
55+
if lib.Output != "" && len(lib.Channels) == 1 && isDerivableOutput(cfg, lib) {
56+
lib.Output = ""
57+
}
58+
}
5459
return yaml.Write(librarianConfigPath, formatConfig(cfg))
5560
}
5661

62+
func isDerivableOutput(cfg *config.Config, lib *config.Library) bool {
63+
derivedOutput := defaultOutput(cfg.Language, lib.Channels[0].Path, cfg.Default.Output)
64+
return lib.Output == derivedOutput
65+
}
66+
5767
func validateLibraries(cfg *config.Config) error {
5868
var (
5969
errs []error

internal/librarian/tidy_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,35 @@ libraries:
150150
t.Errorf("expected %v, got %v", errDuplicateLibraryName, err)
151151
}
152152
}
153+
154+
func TestTidy_DerivableOutput(t *testing.T) {
155+
tempDir := t.TempDir()
156+
t.Chdir(tempDir)
157+
configPath := filepath.Join(tempDir, librarianConfigPath)
158+
configContent := `
159+
language: rust
160+
default:
161+
output: generated/
162+
libraries:
163+
- name: google-cloud-secretmanager-v1
164+
output: generated/cloud/secretmanager/v1
165+
channels:
166+
- path: google/cloud/secretmanager/v1
167+
`
168+
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
169+
t.Fatal(err)
170+
}
171+
if err := RunTidy(); err != nil {
172+
t.Fatal(err)
173+
}
174+
cfg, err := yaml.Read[config.Config](configPath)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
if len(cfg.Libraries) != 1 {
179+
t.Fatalf("expected 1 library, got %d", len(cfg.Libraries))
180+
}
181+
if cfg.Libraries[0].Output != "" {
182+
t.Errorf("expected output to be empty, got %q", cfg.Libraries[0].Output)
183+
}
184+
}

0 commit comments

Comments
 (0)