Skip to content

Commit

Permalink
Search test data directory path dynamically (#2005)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann authored Aug 2, 2024
1 parent 2dd8037 commit 0a92c98
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions clients/feeder/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ func newTestServer(t *testing.T, network *utils.Network) *httptest.Server {
assert.Equal(t, []string{"API_KEY"}, r.Header["X-Throttling-Bypass"])
assert.Equal(t, []string{"Juno/v0.0.1-test Starknet Implementation"}, r.Header["User-Agent"])

wd, err := os.Getwd()
require.NoError(t, err)

base := wd[:strings.LastIndex(wd, "juno")+4]
queryArg := ""
dir := ""
const blockNumberArg = "blockNumber"
Expand Down Expand Up @@ -173,7 +171,11 @@ func newTestServer(t *testing.T, network *utils.Network) *httptest.Server {
return
}

path := filepath.Join(base, "clients", "feeder", "testdata", network.String(), dir, fileName[0]+".json")
dataPath, err := findTargetDirectory("clients/feeder/testdata")
if err != nil {
t.Fatalf("failed to find testdata directory: %v", err)
}
path := filepath.Join(dataPath, network.String(), dir, fileName[0]+".json")
read, err := os.ReadFile(path)
if err != nil {
handleNotFound(dir, queryArg, w)
Expand Down Expand Up @@ -447,3 +449,23 @@ func (c *Client) BlockTrace(ctx context.Context, blockHash string) (*starknet.Bl
}
return traces, nil
}

func findTargetDirectory(targetRelPath string) (string, error) {
root, err := os.Getwd()
if err != nil {
return "", err
}
for {
targetPath := filepath.Join(root, targetRelPath)
if _, err := os.Stat(targetPath); err == nil {
return targetPath, nil
} else if !os.IsNotExist(err) {
return "", err
}
newRoot := filepath.Dir(root)
if newRoot == root {
return "", os.ErrNotExist
}
root = newRoot
}
}

0 comments on commit 0a92c98

Please sign in to comment.