From af6b14a95ce3ad93519114692c25c7258f9f7bd1 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 12 Dec 2024 10:22:29 +0100 Subject: [PATCH] move reading of distros to main func Signed-off-by: Moritz Wiesinger --- githubgen/main.go | 19 ++++++++++--------- githubgen/main_test.go | 18 +++++++++++++++--- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/githubgen/main.go b/githubgen/main.go index 191504b5..548f748b 100644 --- a/githubgen/main.go +++ b/githubgen/main.go @@ -47,7 +47,13 @@ func main() { if len(generators) == 0 { generators = []datatype.Generator{&issueTemplatesGenerator{}, &codeownersGenerator{skipGithub: *skipGithubCheck}} } - if err := run(*folder, *allowlistFilePath, generators); err != nil { + + distributions, err := getDistributions(*folder) + if err != nil { + log.Fatal(err) + } + + if err = run(*folder, *allowlistFilePath, generators, distributions); err != nil { log.Fatal(err) } } @@ -71,13 +77,13 @@ func loadMetadata(filePath string) (datatype.Metadata, error) { return md, nil } -func run(folder string, allowlistFilePath string, generators []datatype.Generator) error { +func run(folder string, allowlistFilePath string, generators []datatype.Generator, distros []datatype.DistributionData) error { components := map[string]datatype.Metadata{} var foldersList []string maxLength := 0 var allCodeowners []string err := filepath.Walk(folder, func(path string, info fs.FileInfo, _ error) error { - if info.Name() == "datatype.Metadata.yaml" { + if info.Name() == "metadata.yaml" { m, err := loadMetadata(path) if err != nil { return err @@ -115,18 +121,13 @@ func run(folder string, allowlistFilePath string, generators []datatype.Generato slices.Sort(allCodeowners) allCodeowners = slices.Compact(allCodeowners) - distributions, err := getDistributions(folder) - if err != nil { - return err - } - data := datatype.GithubData{ Folders: foldersList, Codeowners: allCodeowners, AllowlistFilePath: allowlistFilePath, MaxLength: maxLength, Components: components, - Distributions: distributions, + Distributions: distros, } for _, g := range generators { diff --git a/githubgen/main_test.go b/githubgen/main_test.go index 71b95dc7..1316070a 100644 --- a/githubgen/main_test.go +++ b/githubgen/main_test.go @@ -14,6 +14,7 @@ func Test_run(t *testing.T) { folder string allowlistFilePath string generators fake.MockGenerator + distributions []datatype.DistributionData } tests := []struct { name string @@ -25,17 +26,28 @@ func Test_run(t *testing.T) { args: args{ folder: ".", allowlistFilePath: "cmd/githubgen/allowlist.txt", - generators: fake.MockGenerator{}, + generators: fake.MockGenerator{ + GenerateFunc: func(data datatype.GithubData) error { + return nil + }, + }, + distributions: []datatype.DistributionData{ + { + Name: "my-distro", + URL: "some-url", + Maintainers: nil, + }, + }, }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := run(tt.args.folder, tt.args.allowlistFilePath, []datatype.Generator{&tt.args.generators}); (err != nil) != tt.wantErr { + if err := run(tt.args.folder, tt.args.allowlistFilePath, []datatype.Generator{&tt.args.generators}, tt.args.distributions); (err != nil) != tt.wantErr { t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr) } - require.Equal(t, tt.args.generators.GenerateCalls(), 1) + require.Equal(t, len(tt.args.generators.GenerateCalls()), 1) }) } }