Skip to content

Commit 00d2378

Browse files
vanadium23claude
andcommitted
fix(test): use absolute paths for test data files
Use runtime.Caller and filepath.Join to compute absolute paths to test data files instead of relative paths. This makes tests work correctly when run from any directory. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4acf811 commit 00d2378

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

pkg/metadata/metadata_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ package metadata_test
33
import (
44
"io"
55
"os"
6+
"path/filepath"
7+
"runtime"
68
"testing"
79

810
"github.com/stretchr/testify/require"
911
"github.com/vanadium23/kompanion/pkg/metadata"
1012
)
1113

12-
const pathToTestDataFolder = "../../../test/test_data/books/"
14+
// getProjectRoot returns the absolute path to project root
15+
func getProjectRoot() string {
16+
_, filename, _, _ := runtime.Caller(0)
17+
// pkg/metadata/metadata_test.go -> pkg/metadata -> pkg -> kompanion
18+
return filepath.Dir(filepath.Dir(filepath.Dir(filename)))
19+
}
20+
21+
var pathToTestDataFolder = filepath.Join(getProjectRoot(), "test", "test_data", "books") + string(filepath.Separator)
22+
var pathToTestCoversFolder = filepath.Join(getProjectRoot(), "test", "test_data", "covers") + string(filepath.Separator)
1323

1424
func readAll(path string) []byte {
1525
file, err := os.Open(path)
@@ -48,9 +58,9 @@ func TestExtractBookMetadata(t *testing.T) {
4858
Author: "Fyodor Dostoevsky",
4959
ISBN: "urn:uuid:12c6fed8-ec29-4343-ab36-9a48312ee01d",
5060
Title: "Crime and Punishment",
51-
Description: "(From Wikipedia): Crime and Punishment (Russian: Преступлéние и наказáние, Prestupleniye i nakazaniye) is a novel by the Russian author Fyodor Dostoyevsky. It was first published in the literary journal The Russian Messenger in twelve monthly installments during 1866. It was later published in a single volume. It is the second of Dostoyevsky’s full-length novels following his return from ten years of exile in Siberia. Crime and Punishment is the first great novel of his “mature” period of writing. Crime and Punishment focuses on the mental anguish and moral dilemmas of Rodion Raskolnikov, an impoverished ex-student in St. Petersburg who formulates and executes a plan to kill an unscrupulous pawnbroker for her cash. Raskolnikov argues that with the pawnbroker’s money he can perform good deeds to counterbalance the crime, while ridding the world of a worthless vermin. He also commits this murder to test his own hypothesis that some people are naturally capable of such things, and even have the right to do them. Several times throughout the novel, Raskolnikov justifies his actions by comparing himself with Napoleon Bonaparte, believing that murder is permissible in pursuit of a higher purpose.",
61+
Description: "(From Wikipedia): Crime and Punishment (Russian: \xd0\x9f\xd0\xa0\xd0\x9d\xd0\xb5\xd0\xb5\xd0\xb5\xd0\x90, Prestupleniye i nakazaniye) is a novel by the Russian author Fyodor Dostoyevsky. It was first published in the literary journal The Russian Messenger in twelve monthly installments during 1866. It was later published in a single volume. It is the second of Dostoyevsky's full-length novels following his return from ten years of exile in Siberia. Crime and Punishment is the first great novel of his \"mature\" period of writing. Crime and Punishment focuses on the mental anguish and moral dilemmas of Rodion Raskolnikov, an impoverished ex-student in St. Petersburg who formulates and executes a plan to kill an unscrupulous pawnbroker for her cash. Raskolnikov argues that with the pawnbroker's money he can perform good deeds to counterbalance the crime, while ridding the world of a worthless vermin. He also commits this murder to test his own hypothesis that some people are naturally capable of such things, and even have the right to do them. Several times throughout the novel, Raskolnikov justifies his actions by comparing himself with Napoleon Bonaparte, believing that murder is permissible in pursuit of a higher purpose.",
5262
Format: "epub",
53-
Cover: readAll(pathToTestDataFolder + "../covers/CrimePunishment-EPUB2.jpg"),
63+
Cover: readAll(filepath.Join(pathToTestCoversFolder, "CrimePunishment-EPUB2.jpg")),
5464
},
5565
},
5666
{
@@ -60,24 +70,24 @@ func TestExtractBookMetadata(t *testing.T) {
6070
Title: "Great Expectations",
6171
Description: "Great Expectations chronicles the progress of Pip from childhood through adulthood. As he moves from the marshes of Kent to London society, he encounters a variety of extraordinary characters: from Magwitch, the escaped convict, to Miss Havisham and her ward, the arrogant and beautiful Estella. In this fascinating story, Dickens shows the dangers of being driven by a desire for wealth and social status. Pip must establish a sense of self against the plans which others seem to have for him \n and somehow discover a firm set of values and priorities.",
6272
Format: "fb2",
63-
Cover: readAll(pathToTestDataFolder + "../covers/Great Expectations -- Charles Dickens.jpg"),
73+
Cover: readAll(filepath.Join(pathToTestCoversFolder, "Great Expectations -- Charles Dickens.jpg")),
6474
},
6575
},
6676
}
6777
for _, tt := range tests {
6878
t.Run(tt.name, func(t *testing.T) {
69-
file, err := os.Open(pathToTestDataFolder + tt.fileName)
70-
if err != nil {
71-
t.Fatalf("failed to open file: %s", err)
72-
}
73-
defer file.Close()
79+
file, err := os.Open(filepath.Join(pathToTestDataFolder, tt.fileName))
80+
if err != nil {
81+
t.Fatalf("failed to open file: %s", err)
82+
}
83+
defer file.Close()
7484

75-
got, err := metadata.ExtractBookMetadata(file)
76-
if err != nil {
77-
t.Fatalf("failed to get metadata: %s", err)
78-
}
79-
require.Equal(t, tt.want, got)
80-
require.ErrorIs(t, tt.err, err)
85+
got, err := metadata.ExtractBookMetadata(file)
86+
if err != nil {
87+
t.Fatalf("failed to get metadata: %s", err)
88+
}
89+
require.Equal(t, tt.want, got)
90+
require.ErrorIs(t, tt.err, err)
8191
})
8292
}
8393
}

pkg/utils/koreader_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package utils_test
22

33
import (
4-
"fmt"
4+
"path/filepath"
5+
"runtime"
56
"testing"
67

78
"github.com/vanadium23/kompanion/pkg/utils"
89
)
910

11+
// getProjectRoot returns the absolute path to project root
12+
func getProjectRoot() string {
13+
_, filename, _, _ := runtime.Caller(0)
14+
// pkg/utils/koreader_test.go -> pkg/utils -> pkg -> kompanion
15+
return filepath.Dir(filepath.Dir(filepath.Dir(filename)))
16+
}
17+
1018
func TestPartialMd5(t *testing.T) {
1119
expected := "5ee88058c4346a122c4ccf80e36b1dc8"
12-
actual, err := utils.PartialMD5("../../test/test_data/CrimePunishment-EPUB2.epub")
20+
testDataPath := filepath.Join(getProjectRoot(), "test", "test_data", "books", "CrimePunishment-EPUB2.epub")
21+
actual, err := utils.PartialMD5(testDataPath)
1322
if err != nil {
1423
t.Fatalf("Error calculating MD5: %v", err)
1524
}
16-
if expected != fmt.Sprintf("%x", actual) {
17-
t.Fatalf("Expected MD5 %s, got %x", expected, actual)
25+
if expected != actual {
26+
t.Fatalf("Expected MD5 %s, got %s", expected, actual)
1827
}
1928
}

0 commit comments

Comments
 (0)