package config import ( "os" "path/filepath" "strings" "testing" ) func TestLoadSelectsLocalComposeFilesForAnInstallationInPathsWithSpaces(t *testing.T) { t.Parallel() installationPath, projectDirectory, envFile, override := writeInstallation(t, "local") installation, err := Load(installationPath) if err != nil { t.Fatalf("Load() error = %v", err) } if installation.ProjectDirectory != projectDirectory { t.Errorf("ProjectDirectory = %q, want %q", installation.ProjectDirectory, projectDirectory) } if installation.EnvFile != envFile { t.Errorf("EnvFile = %q, want %q", installation.EnvFile, envFile) } if !strings.Contains(installationPath, "installation folder with spaces") { t.Fatalf("test setup must exercise a path with spaces: %q", installationPath) } want := []string{ filepath.Join(projectDirectory, "compose.yaml"), filepath.Join(projectDirectory, "deploy", "compose.local.yaml"), override, } assertStringsEqual(t, installation.ComposeFiles(), want) } func TestLoadSelectsServerComposeFiles(t *testing.T) { t.Parallel() installationPath, projectDirectory, _, override := writeInstallation(t, "server") installation, err := Load(installationPath) if err != nil { t.Fatalf("Load() error = %v", err) } want := []string{ filepath.Join(projectDirectory, "compose.yaml"), filepath.Join(projectDirectory, "deploy", "compose.server.yaml"), override, } assertStringsEqual(t, installation.ComposeFiles(), want) } func TestLoadRejectsRelativeInstallationPaths(t *testing.T) { t.Parallel() _, err := Load("thothii-installation.yaml") if err == nil || !strings.Contains(err.Error(), "absolute") { t.Fatalf("Load() error = %v, want an absolute-path error", err) } } func writeInstallation(t *testing.T, profile string) (string, string, string, string) { t.Helper() root := filepath.Join(t.TempDir(), "installation folder with spaces") projectDirectory := filepath.Join(root, "project directory with spaces") if err := os.MkdirAll(filepath.Join(projectDirectory, "deploy"), 0o755); err != nil { t.Fatal(err) } for _, name := range []string{"compose.yaml", filepath.Join("deploy", "compose.local.yaml"), filepath.Join("deploy", "compose.server.yaml")} { if err := os.WriteFile(filepath.Join(projectDirectory, name), []byte("services: {}\n"), 0o600); err != nil { t.Fatal(err) } } envFile := filepath.Join(root, "environment file.env") if err := os.WriteFile(envFile, []byte("SAFE_VALUE=1\n"), 0o600); err != nil { t.Fatal(err) } override := filepath.Join(root, "extra override.yaml") if err := os.WriteFile(override, []byte("services: {}\n"), 0o600); err != nil { t.Fatal(err) } installationPath := filepath.Join(root, "thothii-installation.yaml") contents := "profile: " + profile + "\nprojectDirectory: " + projectDirectory + "\nenvFile: " + envFile + "\noverrides:\n - " + override + "\n" if err := os.WriteFile(installationPath, []byte(contents), 0o600); err != nil { t.Fatal(err) } return installationPath, projectDirectory, envFile, override } func assertStringsEqual(t *testing.T, got, want []string) { t.Helper() if len(got) != len(want) { t.Fatalf("length = %d, want %d: got %#v", len(got), len(want), got) } for i := range want { if got[i] != want[i] { t.Errorf("value[%d] = %q, want %q", i, got[i], want[i]) } } }