From 0a92c9824fa15dc8a166d7fd692558d3d685a9d2 Mon Sep 17 00:00:00 2001 From: Ng Wei Han <47109095+weiihann@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:12:24 +0800 Subject: [PATCH] Search test data directory path dynamically (#2005) --- clients/feeder/feeder.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/clients/feeder/feeder.go b/clients/feeder/feeder.go index f938da349e..82e99390ad 100644 --- a/clients/feeder/feeder.go +++ b/clients/feeder/feeder.go @@ -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" @@ -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) @@ -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 + } +}