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:
+36
-2
@@ -214,6 +214,30 @@ type ShortcutItem struct {
|
||||
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
|
||||
func LoadCV(lang string) (*CV, error) {
|
||||
if lang != "en" && lang != "es" {
|
||||
@@ -221,7 +245,12 @@ func LoadCV(lang string) (*CV, error) {
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
data, err := os.ReadFile(filename)
|
||||
filepath, err := findDataFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading file %s: %w", filename, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user