From e8a4c72dd2796604cc4da14ad68dfe8cd00b34db Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Thu, 23 Jan 2025 09:03:29 +0900 Subject: [PATCH] test: add tests --- pkg/vacuum/check_test.go | 44 +++++++ pkg/vacuum/client.go | 12 +- pkg/vacuum/client_test.go | 234 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 pkg/vacuum/check_test.go create mode 100644 pkg/vacuum/client_test.go diff --git a/pkg/vacuum/check_test.go b/pkg/vacuum/check_test.go new file mode 100644 index 000000000..1653d2259 --- /dev/null +++ b/pkg/vacuum/check_test.go @@ -0,0 +1,44 @@ +package vacuum_test + +import ( + "testing" + + "github.com/aquaproj/aqua/v2/pkg/vacuum" +) + +func TestTimestampChecker_Expired(t *testing.T) { + t.Parallel() + data := []struct { + name string + timestamp string + exp bool + }{ + { + name: "expired", + timestamp: "2025-01-13T00:14:59+09:00", + exp: true, + }, + { + name: "not expired", + timestamp: "2025-01-13T00:15:01+09:00", + }, + } + now, err := vacuum.ParseTime("2025-01-20T00:15:00+09:00") + if err != nil { + t.Fatal(err) + } + checker := vacuum.NewTimestampChecker(now, 7) + for _, tt := range data { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + ts, err := vacuum.ParseTime(tt.timestamp) + if err != nil { + t.Fatal(err) + } + a := checker.Expired(ts) + if a != tt.exp { + t.Fatalf("wanted %v, got %v", tt.exp, a) + } + }) + } +} diff --git a/pkg/vacuum/client.go b/pkg/vacuum/client.go index 56a52bb4d..0467b9884 100644 --- a/pkg/vacuum/client.go +++ b/pkg/vacuum/client.go @@ -20,6 +20,14 @@ const ( baseDir = "metadata" ) +func FormatTime(t time.Time) string { + return t.Format(time.RFC3339) +} + +func ParseTime(s string) (time.Time, error) { + return time.Parse(time.RFC3339, s) //nolint:wrapcheck +} + type Client struct { fs afero.Fs rootDir string @@ -69,7 +77,7 @@ func (c *Client) update(file, dir string, timestamp time.Time) error { if err := osfile.MkdirAll(c.fs, dir); err != nil { return fmt.Errorf("create a package metadata directory: %w", err) } - timestampStr := timestamp.Format(time.RFC3339) + timestampStr := FormatTime(timestamp) if err := afero.WriteFile(c.fs, file, []byte(timestampStr+"\n"), filePermission); err != nil { return fmt.Errorf("create a package timestamp file: %w", err) } @@ -90,7 +98,7 @@ func (c *Client) FindAll(logE *logrus.Entry) (map[string]time.Time, error) { if err != nil { return fmt.Errorf("read a timestamp file: %w", err) } - t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(b))) + t, err := ParseTime(strings.TrimSpace(string(b))) if err != nil { logerr.WithError(logE, err).WithField("timestamp_file", path).Warn("a timestamp file is broken, so recreating it") if err := c.Update(path, time.Now()); err != nil { diff --git a/pkg/vacuum/client_test.go b/pkg/vacuum/client_test.go new file mode 100644 index 000000000..a70b7a5c7 --- /dev/null +++ b/pkg/vacuum/client_test.go @@ -0,0 +1,234 @@ +package vacuum_test + +import ( + "path" + "path/filepath" + "testing" + + "github.com/aquaproj/aqua/v2/pkg/config" + "github.com/aquaproj/aqua/v2/pkg/osfile" + "github.com/aquaproj/aqua/v2/pkg/vacuum" + "github.com/google/go-cmp/cmp" + "github.com/sirupsen/logrus" + "github.com/spf13/afero" +) + +func TestClient_Create(t *testing.T) { //nolint:dupl + t.Parallel() + rootDir := "/home/foo/.local/share/aquaproj-aqua" + data := []struct { + name string + pkgPath string + timestamp string + files map[string]string + expPath string + expContent string + }{ + { + name: "create", + pkgPath: "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", + timestamp: "2025-01-10T00:15:00+09:00", + expPath: path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"), + expContent: "2025-01-10T00:15:00+09:00\n", + }, + { + name: "file exists", + pkgPath: "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", + timestamp: "2025-01-10T00:15:00+09:00", + files: map[string]string{ + path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"): "2025-01-01T00:15:00+09:00\n", + }, + expPath: path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"), + expContent: "2025-01-01T00:15:00+09:00\n", + }, + } + for _, tt := range data { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + fs := afero.NewMemMapFs() + for k, v := range tt.files { + if err := osfile.MkdirAll(fs, filepath.Dir(k)); err != nil { + t.Fatal(err) + } + if err := afero.WriteFile(fs, k, []byte(v), 0o644); err != nil { + t.Fatal(err) + } + } + client := vacuum.New(fs, &config.Param{ + RootDir: rootDir, + }) + ts, err := vacuum.ParseTime(tt.timestamp) + if err != nil { + t.Fatal(err) + } + if err := client.Create(tt.pkgPath, ts); err != nil { + t.Fatal(err) + } + b, err := afero.ReadFile(fs, tt.expPath) + if err != nil { + t.Fatal(err) + } + if diff := cmp.Diff(tt.expContent, string(b)); diff != "" { + t.Fatal(diff) + } + }) + } +} + +func TestClient_Update(t *testing.T) { //nolint:dupl + t.Parallel() + rootDir := "/home/foo/.local/share/aquaproj-aqua" + data := []struct { + name string + pkgPath string + timestamp string + files map[string]string + expPath string + expContent string + }{ + { + name: "create", + pkgPath: "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", + timestamp: "2025-01-10T00:15:00+09:00", + expPath: path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"), + expContent: "2025-01-10T00:15:00+09:00\n", + }, + { + name: "file exists (overwrite)", + pkgPath: "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", + timestamp: "2025-01-10T00:15:00+09:00", + files: map[string]string{ + path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"): "2025-01-01T00:15:00+09:00\n", + }, + expPath: path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"), + expContent: "2025-01-10T00:15:00+09:00\n", + }, + } + for _, tt := range data { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + fs := afero.NewMemMapFs() + for k, v := range tt.files { + if err := osfile.MkdirAll(fs, filepath.Dir(k)); err != nil { + t.Fatal(err) + } + if err := afero.WriteFile(fs, k, []byte(v), 0o644); err != nil { + t.Fatal(err) + } + } + client := vacuum.New(fs, &config.Param{ + RootDir: rootDir, + }) + ts, err := vacuum.ParseTime(tt.timestamp) + if err != nil { + t.Fatal(err) + } + if err := client.Update(tt.pkgPath, ts); err != nil { + t.Fatal(err) + } + b, err := afero.ReadFile(fs, tt.expPath) + if err != nil { + t.Fatal(err) + } + if diff := cmp.Diff(tt.expContent, string(b)); diff != "" { + t.Fatal(diff) + } + }) + } +} + +func TestClient_Remove(t *testing.T) { + t.Parallel() + rootDir := "/home/foo/.local/share/aquaproj-aqua" + data := []struct { + name string + pkgPath string + files map[string]string + expPath string + }{ + { + name: "remove", + pkgPath: "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", + files: map[string]string{ + path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"): "2025-01-01T00:15:00+09:00\n", + }, + expPath: path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"), + }, + } + for _, tt := range data { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + fs := afero.NewMemMapFs() + for k, v := range tt.files { + if err := osfile.MkdirAll(fs, filepath.Dir(k)); err != nil { + t.Fatal(err) + } + if err := afero.WriteFile(fs, k, []byte(v), 0o644); err != nil { + t.Fatal(err) + } + } + client := vacuum.New(fs, &config.Param{ + RootDir: rootDir, + }) + if err := client.Remove(tt.pkgPath); err != nil { + t.Fatal(err) + } + if a, err := afero.Exists(fs, tt.expPath); err != nil { + t.Fatal(err) + } else if a { + t.Fatal("the file still exists") + } + }) + } +} + +func TestClient_FindAll(t *testing.T) { + t.Parallel() + rootDir := "/home/foo/.local/share/aquaproj-aqua" + data := []struct { + name string + files map[string]string + exp map[string]string + }{ + { + name: "normal", + files: map[string]string{ + path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip", "timestamp.txt"): "2025-01-01T00:15:00+09:00\n", + path.Join(rootDir, "metadata", "pkgs/github_release/github.com/cli/cli/v2.60.0/gh_2.60.0_macOS_arm64.zip", "timestamp.txt"): "2025-01-20T00:15:00+09:00\n", + }, + exp: map[string]string{ + "pkgs/github_release/github.com/cli/cli/v2.65.0/gh_2.65.0_macOS_arm64.zip": "2025-01-01T00:15:00+09:00", + "pkgs/github_release/github.com/cli/cli/v2.60.0/gh_2.60.0_macOS_arm64.zip": "2025-01-20T00:15:00+09:00", + }, + }, + } + logE := logrus.NewEntry(logrus.New()) + for _, tt := range data { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + fs := afero.NewMemMapFs() + for k, v := range tt.files { + if err := osfile.MkdirAll(fs, filepath.Dir(k)); err != nil { + t.Fatal(err) + } + if err := afero.WriteFile(fs, k, []byte(v), 0o644); err != nil { + t.Fatal(err) + } + } + client := vacuum.New(fs, &config.Param{ + RootDir: rootDir, + }) + timestamps, err := client.FindAll(logE) + if err != nil { + t.Fatal(err) + } + a := make(map[string]string, len(timestamps)) + for k, v := range timestamps { + a[k] = vacuum.FormatTime(v) + } + if diff := cmp.Diff(tt.exp, a); diff != "" { + t.Fatal(diff) + } + }) + } +}