fix: PDF tests data file loading and Chrome installation in CI

- Add findDataFile() helper to search up directory tree for data files
- Fixes tests running from subdirectories (internal/handlers)
- Install Chrome in GitHub Actions for PDF generation tests

This resolves test failures that have existed since PDF tests were introduced:
- Error: 'open data/cv-es.json: no such file or directory'
- Error: 'chrome failed to start'

Tests now properly locate data files from any working directory and
have Chrome available for PDF generation in CI environment.
This commit is contained in:
juanatsap
2025-11-20 13:44:31 +00:00
parent 2c7a467149
commit 295a9948f7
2 changed files with 43 additions and 2 deletions
+7
View File
@@ -30,6 +30,13 @@ jobs:
- name: Verify dependencies - name: Verify dependencies
run: go mod verify run: go mod verify
- name: Install Chrome for PDF tests
run: |
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt-get update
sudo apt-get install -y google-chrome-stable
- name: Run linter - name: Run linter
uses: golangci/golangci-lint-action@v7 uses: golangci/golangci-lint-action@v7
with: with:
+36 -2
View File
@@ -214,6 +214,30 @@ type ShortcutItem struct {
Description string `json:"description"` Description string `json:"description"`
} }
// findDataFile locates a data file by searching up the directory tree
func findDataFile(filename string) (string, error) {
// Try current directory first
if _, err := os.Stat(filename); err == nil {
return filename, nil
}
// Try parent directories (for tests running from subdirectories)
paths := []string{
filename, // Current dir
"../" + filename, // One level up
"../../" + filename, // Two levels up (for tests in internal/handlers)
"../../../" + filename, // Three levels up
}
for _, path := range paths {
if _, err := os.Stat(path); err == nil {
return path, nil
}
}
return "", fmt.Errorf("file not found: %s (searched: current dir, ../, ../../, ../../../)", filename)
}
// LoadCV loads CV data from a JSON file for the specified language // LoadCV loads CV data from a JSON file for the specified language
func LoadCV(lang string) (*CV, error) { func LoadCV(lang string) (*CV, error) {
if lang != "en" && lang != "es" { if lang != "en" && lang != "es" {
@@ -221,7 +245,12 @@ func LoadCV(lang string) (*CV, error) {
} }
filename := fmt.Sprintf("data/cv-%s.json", lang) filename := fmt.Sprintf("data/cv-%s.json", lang)
data, err := os.ReadFile(filename) filepath, err := findDataFile(filename)
if err != nil {
return nil, err
}
data, err := os.ReadFile(filepath)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading file %s: %w", filename, err) return nil, fmt.Errorf("error reading file %s: %w", filename, err)
} }
@@ -252,7 +281,12 @@ func LoadUI(lang string) (*UI, error) {
} }
filename := fmt.Sprintf("data/ui-%s.json", lang) filename := fmt.Sprintf("data/ui-%s.json", lang)
data, err := os.ReadFile(filename) filepath, err := findDataFile(filename)
if err != nil {
return nil, err
}
data, err := os.ReadFile(filepath)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading file %s: %w", filename, err) return nil, fmt.Errorf("error reading file %s: %w", filename, err)
} }