Skip to content

Commit

Permalink
test(update): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 14, 2023
1 parent 45ea671 commit 5733d92
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/config-reader/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *ConfigReaderImpl) ReadToUpdate(configFilePath string, cfg *aqua.Config)
if r.homeDir == "" {
return nil, errHomeDirEmpty
}
rgst.Path = filepath.Join(r.homeDir, rgst.Path[6:])
rgst.Path = filepath.Join(r.homeDir, rgst.Path[6:]) // 6: "$HOME/"
}
if configFileDir == "" {
configFileDir = filepath.Dir(configFilePath)
Expand Down
147 changes: 147 additions & 0 deletions pkg/config-reader/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package reader_test

import (
"testing"

"github.com/aquaproj/aqua/v2/pkg/config"
reader "github.com/aquaproj/aqua/v2/pkg/config-reader"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/testutil"
"github.com/google/go-cmp/cmp"
)

func Test_configReader_ReadToUpdate(t *testing.T) { //nolint:funlen
t.Parallel()
data := []struct {
name string
cfg *aqua.Config
cfgs map[string]*aqua.Config
isErr bool
files map[string]string
configFilePath string
homeDir string
}{
{
name: "file isn't found",
isErr: true,
},
{
name: "normal",
files: map[string]string{
"/home/workspace/foo/aqua.yaml": `registries:
- type: standard
ref: v2.5.0
- type: local
name: local
path: registry.yaml
packages:`,
},
configFilePath: "/home/workspace/foo/aqua.yaml",
cfg: &aqua.Config{
Registries: aqua.Registries{
"standard": {
Type: "github_content",
Name: "standard",
Ref: "v2.5.0",
RepoOwner: "aquaproj",
RepoName: "aqua-registry",
Path: "registry.yaml",
},
"local": {
Type: "local",
Name: "local",
Path: "/home/workspace/foo/registry.yaml",
},
},
Packages: []*aqua.Package{},
},
cfgs: map[string]*aqua.Config{},
},
{
name: "import package",
files: map[string]string{
"/home/workspace/foo/aqua.yaml": `registries:
- type: standard
ref: v2.5.0
packages:
- name: suzuki-shunsuke/[email protected]
- import: aqua-installer.yaml
`,
"/home/workspace/foo/aqua-installer.yaml": `packages:
- name: aquaproj/[email protected]
`,
},
configFilePath: "/home/workspace/foo/aqua.yaml",
cfg: &aqua.Config{
Registries: aqua.Registries{
"standard": {
Type: "github_content",
Name: "standard",
Ref: "v2.5.0",
RepoOwner: "aquaproj",
RepoName: "aqua-registry",
Path: "registry.yaml",
},
},
Packages: []*aqua.Package{
{
Name: "suzuki-shunsuke/ci-info",
Registry: "standard",
Version: "v1.0.0",
},
},
},
cfgs: map[string]*aqua.Config{
"/home/workspace/foo/aqua-installer.yaml": {
Packages: []*aqua.Package{
{
Name: "aquaproj/aqua-installer",
Registry: "standard",
Version: "v1.0.0",
},
},
Registries: aqua.Registries{
"standard": {
Type: "github_content",
Name: "standard",
Ref: "v2.5.0",
RepoOwner: "aquaproj",
RepoName: "aqua-registry",
Path: "registry.yaml",
},
},
},
},
},
}
for _, d := range data {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()
fs, err := testutil.NewFs(d.files)
if err != nil {
t.Fatal(err)
}
reader := reader.New(fs, &config.Param{
HomeDir: d.homeDir,
})
cfg := &aqua.Config{}
cfgs, err := reader.ReadToUpdate(d.configFilePath, cfg)
if err != nil {
if d.isErr {
return
}
t.Fatal(err)
}
if d.isErr {
t.Fatal("error must be returned")
}
if diff := cmp.Diff(d.cfg, cfg); diff != "" {
t.Fatal(diff)
}
if diff := cmp.Diff(d.cfgs, cfgs); diff != "" {
t.Fatal(diff)
}
})
}
}

0 comments on commit 5733d92

Please sign in to comment.