|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestFlatpakInPathAvailable(t *testing.T) { |
| 11 | + result := FlatpakInPath() |
| 12 | + if !result { |
| 13 | + t.Skip("flatpak not in PATH") |
| 14 | + } |
| 15 | + if !result { |
| 16 | + t.Errorf("expected true when flatpak is in PATH") |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func TestFlatpakInPathUnavailable(t *testing.T) { |
| 21 | + tempDir := t.TempDir() |
| 22 | + t.Setenv("PATH", tempDir) |
| 23 | + |
| 24 | + result := FlatpakInPath() |
| 25 | + if result { |
| 26 | + t.Errorf("expected false when flatpak not in PATH, got true") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestFlatpakExistsValidPackage(t *testing.T) { |
| 31 | + if !FlatpakInPath() { |
| 32 | + t.Skip("flatpak not in PATH") |
| 33 | + } |
| 34 | + |
| 35 | + result := FlatpakExists("com.nonexistent.package.test") |
| 36 | + if result { |
| 37 | + t.Logf("package exists (unexpected but not an error)") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestFlatpakExistsNoFlatpak(t *testing.T) { |
| 42 | + tempDir := t.TempDir() |
| 43 | + t.Setenv("PATH", tempDir) |
| 44 | + |
| 45 | + result := FlatpakExists("any.package.name") |
| 46 | + if result { |
| 47 | + t.Errorf("expected false when flatpak not in PATH, got true") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestFlatpakSearchBySubstringNoFlatpak(t *testing.T) { |
| 52 | + tempDir := t.TempDir() |
| 53 | + t.Setenv("PATH", tempDir) |
| 54 | + |
| 55 | + result := FlatpakSearchBySubstring("test") |
| 56 | + if result { |
| 57 | + t.Errorf("expected false when flatpak not in PATH, got true") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestFlatpakSearchBySubstringNonexistent(t *testing.T) { |
| 62 | + if !FlatpakInPath() { |
| 63 | + t.Skip("flatpak not in PATH") |
| 64 | + } |
| 65 | + |
| 66 | + result := FlatpakSearchBySubstring("ThisIsAVeryUnlikelyPackageName12345") |
| 67 | + if result { |
| 68 | + t.Errorf("expected false for nonexistent package substring") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestFlatpakInstallationDirNoFlatpak(t *testing.T) { |
| 73 | + tempDir := t.TempDir() |
| 74 | + t.Setenv("PATH", tempDir) |
| 75 | + |
| 76 | + _, err := FlatpakInstallationDir("any.package.name") |
| 77 | + if err == nil { |
| 78 | + t.Errorf("expected error when flatpak not in PATH") |
| 79 | + } |
| 80 | + if err != nil && !strings.Contains(err.Error(), "not found in PATH") { |
| 81 | + t.Errorf("expected 'not found in PATH' error, got: %v", err) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestFlatpakInstallationDirNonexistent(t *testing.T) { |
| 86 | + if !FlatpakInPath() { |
| 87 | + t.Skip("flatpak not in PATH") |
| 88 | + } |
| 89 | + |
| 90 | + _, err := FlatpakInstallationDir("com.nonexistent.package.test") |
| 91 | + if err == nil { |
| 92 | + t.Errorf("expected error for nonexistent package") |
| 93 | + } |
| 94 | + if err != nil && !strings.Contains(err.Error(), "not installed") { |
| 95 | + t.Errorf("expected 'not installed' error, got: %v", err) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestFlatpakInstallationDirValid(t *testing.T) { |
| 100 | + if !FlatpakInPath() { |
| 101 | + t.Skip("flatpak not in PATH") |
| 102 | + } |
| 103 | + |
| 104 | + // This test requires a known installed flatpak |
| 105 | + // We can't guarantee any specific flatpak is installed, |
| 106 | + // so we'll skip if we can't find a common one |
| 107 | + commonFlatpaks := []string{ |
| 108 | + "org.mozilla.firefox", |
| 109 | + "org.gnome.Calculator", |
| 110 | + "org.freedesktop.Platform", |
| 111 | + } |
| 112 | + |
| 113 | + var testPackage string |
| 114 | + for _, pkg := range commonFlatpaks { |
| 115 | + if FlatpakExists(pkg) { |
| 116 | + testPackage = pkg |
| 117 | + break |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if testPackage == "" { |
| 122 | + t.Skip("no common flatpak packages found for testing") |
| 123 | + } |
| 124 | + |
| 125 | + result, err := FlatpakInstallationDir(testPackage) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("unexpected error: %v", err) |
| 128 | + } |
| 129 | + if result == "" { |
| 130 | + t.Errorf("expected non-empty installation directory") |
| 131 | + } |
| 132 | + if !strings.Contains(result, testPackage) { |
| 133 | + t.Logf("installation directory %s doesn't contain package name (may be expected)", result) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestFlatpakExistsCommandFailure(t *testing.T) { |
| 138 | + if !FlatpakInPath() { |
| 139 | + t.Skip("flatpak not in PATH") |
| 140 | + } |
| 141 | + |
| 142 | + // Mock a failing flatpak command through PATH interception |
| 143 | + tempDir := t.TempDir() |
| 144 | + fakeFlatpak := filepath.Join(tempDir, "flatpak") |
| 145 | + |
| 146 | + script := "#!/bin/sh\nexit 1\n" |
| 147 | + err := os.WriteFile(fakeFlatpak, []byte(script), 0755) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("failed to create fake flatpak: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + originalPath := os.Getenv("PATH") |
| 153 | + t.Setenv("PATH", tempDir+":"+originalPath) |
| 154 | + |
| 155 | + result := FlatpakExists("test.package") |
| 156 | + if result { |
| 157 | + t.Errorf("expected false when flatpak command fails, got true") |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestFlatpakSearchBySubstringCommandFailure(t *testing.T) { |
| 162 | + if !FlatpakInPath() { |
| 163 | + t.Skip("flatpak not in PATH") |
| 164 | + } |
| 165 | + |
| 166 | + // Mock a failing flatpak command through PATH interception |
| 167 | + tempDir := t.TempDir() |
| 168 | + fakeFlatpak := filepath.Join(tempDir, "flatpak") |
| 169 | + |
| 170 | + script := "#!/bin/sh\nexit 1\n" |
| 171 | + err := os.WriteFile(fakeFlatpak, []byte(script), 0755) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("failed to create fake flatpak: %v", err) |
| 174 | + } |
| 175 | + |
| 176 | + originalPath := os.Getenv("PATH") |
| 177 | + t.Setenv("PATH", tempDir+":"+originalPath) |
| 178 | + |
| 179 | + result := FlatpakSearchBySubstring("test") |
| 180 | + if result { |
| 181 | + t.Errorf("expected false when flatpak command fails, got true") |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +func TestFlatpakInstallationDirCommandFailure(t *testing.T) { |
| 186 | + if !FlatpakInPath() { |
| 187 | + t.Skip("flatpak not in PATH") |
| 188 | + } |
| 189 | + |
| 190 | + // Mock a failing flatpak command through PATH interception |
| 191 | + tempDir := t.TempDir() |
| 192 | + fakeFlatpak := filepath.Join(tempDir, "flatpak") |
| 193 | + |
| 194 | + script := "#!/bin/sh\nexit 1\n" |
| 195 | + err := os.WriteFile(fakeFlatpak, []byte(script), 0755) |
| 196 | + if err != nil { |
| 197 | + t.Fatalf("failed to create fake flatpak: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + originalPath := os.Getenv("PATH") |
| 201 | + t.Setenv("PATH", tempDir+":"+originalPath) |
| 202 | + |
| 203 | + _, err = FlatpakInstallationDir("test.package") |
| 204 | + if err == nil { |
| 205 | + t.Errorf("expected error when flatpak command fails") |
| 206 | + } |
| 207 | + if err != nil && !strings.Contains(err.Error(), "not installed") { |
| 208 | + t.Errorf("expected 'not installed' error, got: %v", err) |
| 209 | + } |
| 210 | +} |
0 commit comments