Skip to content

Commit 1ec1f6e

Browse files
authored
chore(internal/librarian): refactor unit tests (#6557)
For unit tests of `applyDefaults`, split it into success and error cases. See https://github.com/googleapis/librarian/blob/main/doc/howwewritego.md#separate-error-tests
1 parent d6d5054 commit 1ec1f6e

2 files changed

Lines changed: 39 additions & 15 deletions

File tree

internal/librarian/library.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package librarian
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"maps"
2021
"strings"
@@ -28,6 +29,10 @@ import (
2829
"github.com/googleapis/librarian/internal/librarian/swift"
2930
)
3031

32+
var (
33+
errNoExplicitOutput = errors.New("library requires an explicit output path")
34+
)
35+
3136
// fillDefaults populates empty library fields from the provided defaults.
3237
func fillDefaults(lib *config.Library, d *config.Default) *config.Library {
3338
if d == nil {
@@ -288,7 +293,7 @@ func applyDefaults(language string, lib *config.Library, defaults *config.Defaul
288293
}
289294
if lib.Output == "" {
290295
if isMixedLibrary(language, lib) {
291-
return nil, fmt.Errorf("library %q requires an explicit output path", lib.Name)
296+
return nil, fmt.Errorf("%s: %w", lib.Name, errNoExplicitOutput)
292297
}
293298
var apiPath string
294299
if len(lib.APIs) > 0 {

internal/librarian/library_test.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package librarian
1616

1717
import (
18+
"errors"
1819
"testing"
1920

2021
"github.com/google/go-cmp/cmp"
@@ -626,15 +627,14 @@ func TestFillDefaults_Go(t *testing.T) {
626627
}
627628
}
628629

629-
func TestPrepareLibrary(t *testing.T) {
630+
func TestApplyDefaults(t *testing.T) {
630631
for _, test := range []struct {
631632
name string
632633
language string
633634
output string
634635
rust *config.RustCrate
635636
apis []*config.API
636637
wantOutput string
637-
wantErr bool
638638
wantAPIPath string
639639
}{
640640
{
@@ -671,12 +671,6 @@ func TestPrepareLibrary(t *testing.T) {
671671
apis: nil,
672672
wantOutput: "src/storage/test/v1",
673673
},
674-
{
675-
name: "veneer without output returns error",
676-
language: config.LanguageRust,
677-
rust: &config.RustCrate{Modules: []*config.RustModule{{APIPath: "google/storage/v2"}}},
678-
wantErr: true,
679-
},
680674
{
681675
name: "veneer with explicit output succeeds",
682676
language: config.LanguageRust,
@@ -708,12 +702,6 @@ func TestPrepareLibrary(t *testing.T) {
708702
Output: "src/generated",
709703
}
710704
got, err := applyDefaults(test.language, lib, defaults)
711-
if test.wantErr {
712-
if err == nil {
713-
t.Fatal("expected error, got nil")
714-
}
715-
return
716-
}
717705
if err != nil {
718706
t.Fatal(err)
719707
}
@@ -730,6 +718,37 @@ func TestPrepareLibrary(t *testing.T) {
730718
}
731719
}
732720

721+
func TestApplyDefaults_Error(t *testing.T) {
722+
for _, test := range []struct {
723+
name string
724+
language string
725+
lib *config.Library
726+
wantErr error
727+
}{
728+
{
729+
name: "veneer without output returns error",
730+
language: config.LanguageRust,
731+
lib: &config.Library{
732+
Name: "storage",
733+
Rust: &config.RustCrate{
734+
Modules: []*config.RustModule{{APIPath: "google/storage/v2"}},
735+
},
736+
},
737+
wantErr: errNoExplicitOutput,
738+
},
739+
} {
740+
t.Run(test.name, func(t *testing.T) {
741+
defaults := &config.Default{
742+
Output: "src/generated",
743+
}
744+
_, err := applyDefaults(test.language, test.lib, defaults)
745+
if !errors.Is(err, test.wantErr) {
746+
t.Errorf("got error %v, want %v", err, test.wantErr)
747+
}
748+
})
749+
}
750+
}
751+
733752
func TestCanDeriveAPIPath(t *testing.T) {
734753
for _, test := range []struct {
735754
name string

0 commit comments

Comments
 (0)