From 6a5c1a2f42abc7fdc21e671da5b8a227816949f4 Mon Sep 17 00:00:00 2001 From: AYM1607 Date: Thu, 31 Oct 2024 02:01:07 +0000 Subject: [PATCH] adds tests for module prerelease version constraints with a v prefix Signed-off-by: AYM1607 --- internal/initwd/module_install_test.go | 84 ++++++++++--------- .../root.tf | 7 ++ .../root.tf | 7 ++ .../root.tf | 2 +- .../prerelease-version-constraint/root.tf | 2 +- 5 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 internal/initwd/testdata/prerelease-version-constraint-match-prefix-eq/root.tf create mode 100644 internal/initwd/testdata/prerelease-version-constraint-match-prefix/root.tf diff --git a/internal/initwd/module_install_test.go b/internal/initwd/module_install_test.go index a76a9d65a1..b0750dbb55 100644 --- a/internal/initwd/module_install_test.go +++ b/internal/initwd/module_install_test.go @@ -235,57 +235,61 @@ func TestModuleInstaller_explicitPackageBoundary(t *testing.T) { } } -func TestModuleInstaller_ExactMatchPrerelease(t *testing.T) { +func TestModuleInstaller_Prerelease(t *testing.T) { if os.Getenv("TF_ACC") == "" { t.Skip("this test accesses registry.opentofu.org and github.com; set TF_ACC=1 to run it") } - fixtureDir := filepath.Clean("testdata/prerelease-version-constraint-match") - dir, done := tempChdir(t, fixtureDir) - defer done() - - hooks := &testInstallHooks{} - - modulesDir := filepath.Join(dir, ".terraform/modules") - - loader, close := configload.NewLoaderForTests(t) - defer close() - inst := NewModuleInstaller(modulesDir, loader, registry.NewClient(nil, nil)) - cfg, diags := inst.InstallModules(context.Background(), ".", "tests", false, false, hooks, configs.RootModuleCallForTesting()) - - if diags.HasErrors() { - t.Fatalf("found unexpected errors: %s", diags.Err()) + testCases := []struct { + name string + modulePath string + expectedVersion string + }{ + { + name: "exact match", + modulePath: "testdata/prerelease-version-constraint-match", + expectedVersion: "v0.0.3-alpha.1", + }, + { + name: "exact match v prefix", + modulePath: "testdata/prerelease-version-constraint-match-prefix", + expectedVersion: "v0.0.3-alpha.1", + }, + { + name: "exact match v prefix eq selector", + modulePath: "testdata/prerelease-version-constraint-match-prefix-eq", + expectedVersion: "v0.0.3-alpha.1", + }, + { + name: "partial match", + modulePath: "testdata/prerelease-version-constraint", + expectedVersion: "v0.0.2", + }, } - if !cfg.Children["acctest_exact"].Version.Equal(version.Must(version.NewVersion("v0.0.3-alpha.1"))) { - t.Fatalf("expected version %s but found version %s", "v0.0.3-alpha.1", cfg.Version.String()) - } -} + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + fixtureDir := filepath.Clean(tc.modulePath) + dir, done := tempChdir(t, fixtureDir) + defer done() -func TestModuleInstaller_PartialMatchPrerelease(t *testing.T) { - if os.Getenv("TF_ACC") == "" { - t.Skip("this test accesses registry.opentofu.org and github.com; set TF_ACC=1 to run it") - } + hooks := &testInstallHooks{} - fixtureDir := filepath.Clean("testdata/prerelease-version-constraint") - dir, done := tempChdir(t, fixtureDir) - defer done() + modulesDir := filepath.Join(dir, ".terraform/modules") - hooks := &testInstallHooks{} + loader, close := configload.NewLoaderForTests(t) + defer close() + inst := NewModuleInstaller(modulesDir, loader, registry.NewClient(nil, nil)) + cfg, diags := inst.InstallModules(context.Background(), ".", "tests", false, false, hooks, configs.RootModuleCallForTesting()) - modulesDir := filepath.Join(dir, ".terraform/modules") + if diags.HasErrors() { + t.Fatalf("found unexpected errors: %s", diags.Err()) + } - loader, close := configload.NewLoaderForTests(t) - defer close() - inst := NewModuleInstaller(modulesDir, loader, registry.NewClient(nil, nil)) - cfg, diags := inst.InstallModules(context.Background(), ".", "tests", false, false, hooks, configs.RootModuleCallForTesting()) - - if diags.HasErrors() { - t.Fatalf("found unexpected errors: %s", diags.Err()) - } - - if !cfg.Children["acctest_partial"].Version.Equal(version.Must(version.NewVersion("v0.0.2"))) { - t.Fatalf("expected version %s but found version %s", "v0.0.2", cfg.Version.String()) + if !cfg.Children["acctest"].Version.Equal(version.Must(version.NewVersion(tc.expectedVersion))) { + t.Fatalf("expected version %s but found version %s", tc.expectedVersion, cfg.Version.String()) + } + }) } } diff --git a/internal/initwd/testdata/prerelease-version-constraint-match-prefix-eq/root.tf b/internal/initwd/testdata/prerelease-version-constraint-match-prefix-eq/root.tf new file mode 100644 index 0000000000..afcd9e48d4 --- /dev/null +++ b/internal/initwd/testdata/prerelease-version-constraint-match-prefix-eq/root.tf @@ -0,0 +1,7 @@ +# We expect this test to download the requested version because it is an exact +# match for a prerelease version. + +module "acctest" { + source = "hashicorp/module-installer-acctest/aws" + version = "=v0.0.3-alpha.1" +} diff --git a/internal/initwd/testdata/prerelease-version-constraint-match-prefix/root.tf b/internal/initwd/testdata/prerelease-version-constraint-match-prefix/root.tf new file mode 100644 index 0000000000..2de14a3152 --- /dev/null +++ b/internal/initwd/testdata/prerelease-version-constraint-match-prefix/root.tf @@ -0,0 +1,7 @@ +# We expect this test to download the requested version because it is an exact +# match for a prerelease version. + +module "acctest" { + source = "hashicorp/module-installer-acctest/aws" + version = "v0.0.3-alpha.1" +} diff --git a/internal/initwd/testdata/prerelease-version-constraint-match/root.tf b/internal/initwd/testdata/prerelease-version-constraint-match/root.tf index b68baf770e..fb7d353b3b 100644 --- a/internal/initwd/testdata/prerelease-version-constraint-match/root.tf +++ b/internal/initwd/testdata/prerelease-version-constraint-match/root.tf @@ -1,7 +1,7 @@ # We expect this test to download the requested version because it is an exact # match for a prerelease version. -module "acctest_exact" { +module "acctest" { source = "hashicorp/module-installer-acctest/aws" version = "=0.0.3-alpha.1" } diff --git a/internal/initwd/testdata/prerelease-version-constraint/root.tf b/internal/initwd/testdata/prerelease-version-constraint/root.tf index 8ff3dd68da..0671ee5246 100644 --- a/internal/initwd/testdata/prerelease-version-constraint/root.tf +++ b/internal/initwd/testdata/prerelease-version-constraint/root.tf @@ -2,7 +2,7 @@ # specified version even with the equality because the specified version is a # prerelease. -module "acctest_partial" { +module "acctest" { source = "hashicorp/module-installer-acctest/aws" version = "<=0.0.3-alpha.1" }