Skip to content

Commit 85c466c

Browse files
authored
feat(): detect chain directory dynamically (#2292)
- Detect project root based on `chain` directory - Better detection of chain directory location - Allow to move source files more easily
1 parent 45dce9b commit 85c466c

File tree

14 files changed

+194
-115
lines changed

14 files changed

+194
-115
lines changed

cmd/gossamer/import_runtime.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ import (
1010
"path/filepath"
1111

1212
"github.com/ChainSafe/gossamer/lib/genesis"
13+
"github.com/ChainSafe/gossamer/lib/utils"
1314
)
1415

15-
var defaultGenesisSpecPath = "./chain/gssmr/genesis-spec.json"
16-
1716
func createGenesisWithRuntime(fp string) (string, error) {
1817
runtime, err := os.ReadFile(filepath.Clean(fp))
1918
if err != nil {
2019
return "", err
2120
}
2221

23-
genesis, err := genesis.NewGenesisSpecFromJSON(defaultGenesisSpecPath)
22+
genesisPath, err := utils.GetGssmrGenesisPath()
23+
if err != nil {
24+
return "", fmt.Errorf("cannot find gssmr genesis path: %w", err)
25+
}
26+
27+
genesis, err := genesis.NewGenesisSpecFromJSON(genesisPath)
2428
if err != nil {
2529
return "", err
2630
}

cmd/gossamer/import_runtime_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
)
1717

1818
func TestCreateGenesisWithRuntime(t *testing.T) {
19-
defaultGenesisSpecPath = "../../chain/gssmr/genesis-spec.json"
20-
2119
testCode := []byte("somecode")
2220
testHex := common.BytesToHex(testCode)
2321

cmd/gossamer/main_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"os"
1212
"os/exec"
13+
"path/filepath"
1314
"sync"
1415
"syscall"
1516
"testing"
@@ -195,10 +196,17 @@ func TestMain(m *testing.M) {
195196
if reexec.Init() {
196197
return
197198
}
198-
defaultGssmrConfigPath = "../../chain/gssmr/config.toml"
199-
defaultKusamaConfigPath = "../../chain/kusama/config.toml"
200-
defaultPolkadotConfigPath = "../../chain/polkadot/config.toml"
201-
defaultDevConfigPath = "../../chain/dev/config.toml"
199+
200+
rootPath, err := utils.GetProjectRootPath()
201+
if err != nil {
202+
panic(err)
203+
}
204+
205+
defaultGssmrConfigPath = filepath.Join(rootPath, "./chain/gssmr/config.toml")
206+
defaultKusamaConfigPath = filepath.Join(rootPath, "./chain/kusama/config.toml")
207+
defaultPolkadotConfigPath = filepath.Join(rootPath, "./chain/polkadot/config.toml")
208+
defaultDevConfigPath = filepath.Join(rootPath, "./chain/dev/config.toml")
209+
202210
os.Exit(m.Run())
203211
}
204212

@@ -217,7 +225,7 @@ func TestInvalidCommand(t *testing.T) {
217225
}
218226

219227
func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
220-
genesisPath := utils.GetGssmrGenesisRawPath()
228+
genesisPath := utils.GetGssmrGenesisRawPathTest(t)
221229

222230
tempDir := t.TempDir()
223231

@@ -255,11 +263,12 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
255263
}
256264

257265
func TestBuildSpecCommandWithOutput(t *testing.T) {
258-
tmpOutputfile := "/tmp/raw-genesis-spec-output.json"
266+
const tmpOutputfile = "/tmp/raw-genesis-spec-output.json"
267+
259268
buildSpecCommand := runTestGossamer(t,
260269
"build-spec",
261270
"--raw",
262-
"--genesis-spec", "../../chain/gssmr/genesis-spec.json",
271+
"--genesis-spec", utils.GetGssmrGenesisPathTest(t),
263272
"--output", tmpOutputfile)
264273

265274
time.Sleep(5 * time.Second)

cmd/gossamer/toml_config_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44
package main
55

66
import (
7+
"path/filepath"
78
"testing"
89

910
"github.com/ChainSafe/gossamer/dot"
11+
"github.com/ChainSafe/gossamer/lib/utils"
1012

1113
"github.com/stretchr/testify/require"
1214
)
1315

14-
const GssmrConfigPath = "../../chain/gssmr/config.toml"
15-
const GssmrGenesisPath = "../../chain/gssmr/genesis.json"
16-
17-
const KusamaConfigPath = "../../chain/kusama/config.toml"
18-
const KusamaGenesisPath = "../../chain/kusama/genesis.json"
19-
2016
// TestLoadConfig tests loading a toml configuration file
2117
func TestLoadConfig(t *testing.T) {
2218
cfg, cfgFile := newTestConfigWithFile(t)
@@ -40,12 +36,15 @@ func TestLoadConfigGssmr(t *testing.T) {
4036
require.NotNil(t, cfg)
4137

4238
cfg.Global.BasePath = t.TempDir()
43-
cfg.Init.Genesis = GssmrGenesisPath
39+
cfg.Init.Genesis = utils.GetGssmrGenesisPathTest(t)
4440

4541
err := dot.InitNode(cfg)
4642
require.Nil(t, err)
4743

48-
err = loadConfig(dotConfigToToml(cfg), GssmrConfigPath)
44+
projectRootPath := utils.GetProjectRootPathTest(t)
45+
gssmrConfigPath := filepath.Join(projectRootPath, "./chain/gssmr/config.toml")
46+
47+
err = loadConfig(dotConfigToToml(cfg), gssmrConfigPath)
4948
require.Nil(t, err)
5049
require.NotNil(t, cfg)
5150
}
@@ -55,11 +54,14 @@ func TestLoadConfigKusama(t *testing.T) {
5554
require.NotNil(t, cfg)
5655

5756
cfg.Global.BasePath = t.TempDir()
58-
cfg.Init.Genesis = KusamaGenesisPath
57+
cfg.Init.Genesis = utils.GetKusamaGenesisPath(t)
5958

6059
err := dot.InitNode(cfg)
6160
require.Nil(t, err)
6261

63-
err = loadConfig(dotConfigToToml(cfg), KusamaConfigPath)
62+
projectRootPath := utils.GetProjectRootPathTest(t)
63+
kusamaConfigPath := filepath.Join(projectRootPath, "./chain/kusama/config.toml")
64+
65+
err = loadConfig(dotConfigToToml(cfg), kusamaConfigPath)
6466
require.Nil(t, err)
6567
}

dot/build_spec_integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"testing"
1515

1616
"github.com/ChainSafe/gossamer/lib/genesis"
17-
"github.com/ChainSafe/gossamer/lib/runtime"
17+
"github.com/ChainSafe/gossamer/lib/utils"
1818
"github.com/stretchr/testify/require"
1919
)
2020

@@ -85,7 +85,7 @@ func TestWriteGenesisSpecFile(t *testing.T) {
8585
t.Parallel()
8686

8787
cfg := NewTestConfig(t)
88-
cfg.Init.Genesis = runtime.GetAbsolutePath("../chain/gssmr/genesis.json")
88+
cfg.Init.Genesis = utils.GetGssmrGenesisRawPathTest(t)
8989

9090
expected, err := genesis.NewGenesisFromJSONRaw(cfg.Init.Genesis)
9191
require.NoError(t, err)
@@ -124,7 +124,7 @@ func TestBuildFromDB(t *testing.T) {
124124

125125
// setup expected
126126
cfg := NewTestConfig(t)
127-
cfg.Init.Genesis = runtime.GetAbsolutePath("../chain/gssmr/genesis.json")
127+
cfg.Init.Genesis = utils.GetGssmrGenesisRawPathTest(t)
128128
expected, err := genesis.NewGenesisFromJSONRaw(cfg.Init.Genesis)
129129
require.NoError(t, err)
130130
// initialise node (initialise state database and load genesis data)

dot/rpc/modules/sync_state_integration_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import (
1313
"testing"
1414

1515
"github.com/ChainSafe/gossamer/lib/genesis"
16+
"github.com/ChainSafe/gossamer/lib/utils"
1617
"github.com/stretchr/testify/require"
1718
)
1819

19-
const GssmrGenesisPath = "../../../chain/gssmr/genesis.json"
20-
2120
func TestSyncStateModule(t *testing.T) {
22-
fp, err := filepath.Abs(GssmrGenesisPath)
23-
require.NoError(t, err)
21+
fp := utils.GetGssmrGenesisRawPathTest(t)
2422

2523
data, err := ioutil.ReadFile(filepath.Clean(fp))
2624
require.NoError(t, err)

dot/sync/syncer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func newTestSyncer(t *testing.T) *Service {
156156
}
157157

158158
func newTestGenesisWithTrieAndHeader(t *testing.T) (*genesis.Genesis, *trie.Trie, *types.Header) {
159-
fp := "../../chain/gssmr/genesis.json"
159+
fp := utils.GetGssmrGenesisRawPathTest(t)
160160
gen, err := genesis.NewGenesisFromJSONRaw(fp)
161161
require.NoError(t, err)
162162

dot/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
// newTestGenesis returns a test genesis instance using "gssmr" raw data
2828
func newTestGenesis(t *testing.T) *genesis.Genesis {
29-
fp := utils.GetGssmrGenesisRawPath()
29+
fp := utils.GetGssmrGenesisRawPathTest(t)
3030

3131
gssmrGen, err := genesis.NewGenesisFromJSONRaw(fp)
3232
require.NoError(t, err)
@@ -44,7 +44,7 @@ func newTestGenesis(t *testing.T) *genesis.Genesis {
4444
func NewTestGenesisRawFile(t *testing.T, cfg *Config) (filename string) {
4545
filename = filepath.Join(t.TempDir(), "genesis.json")
4646

47-
fp := utils.GetGssmrGenesisRawPath()
47+
fp := utils.GetGssmrGenesisRawPathTest(t)
4848

4949
gssmrGen, err := genesis.NewGenesisFromJSONRaw(fp)
5050
require.Nil(t, err)
@@ -68,7 +68,7 @@ func NewTestGenesisRawFile(t *testing.T, cfg *Config) (filename string) {
6868

6969
// newTestGenesisFile returns a human-readable test genesis file using "gssmr" human readable data
7070
func newTestGenesisFile(t *testing.T, cfg *Config) (filename string) {
71-
fp := utils.GetGssmrGenesisPath()
71+
fp := utils.GetGssmrGenesisPathTest(t)
7272

7373
gssmrGen, err := genesis.NewGenesisFromJSON(fp, 0)
7474
require.Nil(t, err)

lib/genesis/test_utils.go

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ package genesis
55

66
import (
77
"encoding/json"
8-
"errors"
98
"math/big"
109
"os"
11-
"path"
1210
"path/filepath"
13-
"runtime"
1411
"testing"
1512

1613
"github.com/ChainSafe/gossamer/dot/types"
1714
"github.com/ChainSafe/gossamer/lib/common"
1815
"github.com/ChainSafe/gossamer/lib/trie"
16+
"github.com/ChainSafe/gossamer/lib/utils"
1917
"github.com/stretchr/testify/require"
2018
)
2119

@@ -101,40 +99,9 @@ func CreateTestGenesisJSONFile(t *testing.T, asRaw bool) (filename string) {
10199
return filename
102100
}
103101

104-
// getAbsolutePath returns the absolute path concatenated with pathFromRoot
105-
func getAbsolutePath(t *testing.T, pathFromRoot string) string {
106-
t.Helper()
107-
108-
_, fullpath, _, _ := runtime.Caller(0)
109-
finderPath := path.Dir(fullpath)
110-
111-
const searchingFor = "go.mod"
112-
for {
113-
filepathToCheck := path.Join(finderPath, searchingFor)
114-
_, err := os.Stat(filepathToCheck)
115-
116-
fileNotFound := errors.Is(err, os.ErrNotExist)
117-
if fileNotFound {
118-
previousFinderPath := finderPath
119-
finderPath = path.Dir(finderPath)
120-
121-
if finderPath == previousFinderPath {
122-
t.Fatal(t, "cannot find project root")
123-
}
124-
125-
continue
126-
}
127-
128-
require.NoError(t, err)
129-
break
130-
}
131-
132-
return filepath.Join(finderPath, pathFromRoot)
133-
}
134-
135102
// NewTestGenesisWithTrieAndHeader generates genesis, genesis trie and genesis header
136103
func NewTestGenesisWithTrieAndHeader(t *testing.T) (*Genesis, *trie.Trie, *types.Header) {
137-
genesisPath := getAbsolutePath(t, "chain/gssmr/genesis.json")
104+
genesisPath := utils.GetGssmrGenesisRawPathTest(t)
138105
gen, err := NewGenesisFromJSONRaw(genesisPath)
139106
require.NoError(t, err)
140107

@@ -144,7 +111,7 @@ func NewTestGenesisWithTrieAndHeader(t *testing.T) (*Genesis, *trie.Trie, *types
144111

145112
// NewDevGenesisWithTrieAndHeader generates test dev genesis, genesis trie and genesis header
146113
func NewDevGenesisWithTrieAndHeader(t *testing.T) (*Genesis, *trie.Trie, *types.Header) {
147-
genesisPath := getAbsolutePath(t, "chain/dev/genesis.json")
114+
genesisPath := utils.GetDevGenesisPath(t)
148115

149116
gen, err := NewGenesisFromJSONRaw(genesisPath)
150117
require.NoError(t, err)

lib/runtime/life/exports_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import (
1515
"github.com/ChainSafe/gossamer/lib/runtime"
1616
"github.com/ChainSafe/gossamer/lib/runtime/storage"
1717
"github.com/ChainSafe/gossamer/lib/trie"
18+
"github.com/ChainSafe/gossamer/lib/utils"
1819
"github.com/ChainSafe/gossamer/pkg/scale"
1920
"github.com/stretchr/testify/require"
2021
)
2122

2223
func newInstanceFromGenesis(t *testing.T) runtime.Instance {
23-
gen, err := genesis.NewGenesisFromJSONRaw("../../../chain/gssmr/genesis.json")
24+
genesisPath := utils.GetGssmrGenesisRawPathTest(t)
25+
gen, err := genesis.NewGenesisFromJSONRaw(genesisPath)
2426
require.NoError(t, err)
2527

2628
genTrie, err := genesis.NewTrieFromGenesis(gen)
@@ -209,7 +211,8 @@ func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) {
209211
block := buildBlock(t, instance)
210212

211213
// reset state back to parent state before executing
212-
gen, err := genesis.NewGenesisFromJSONRaw("../../../chain/gssmr/genesis.json")
214+
genesisPath := utils.GetGssmrGenesisRawPathTest(t)
215+
gen, err := genesis.NewGenesisFromJSONRaw(genesisPath)
213216
require.NoError(t, err)
214217
genTrie, err := genesis.NewTrieFromGenesis(gen)
215218
require.NoError(t, err)
@@ -222,7 +225,8 @@ func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) {
222225
}
223226

224227
func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) {
225-
gen, err := genesis.NewGenesisFromJSONRaw("../../../chain/kusama/genesis.json")
228+
genesisPath := utils.GetKusamaGenesisPath(t)
229+
gen, err := genesis.NewGenesisFromJSONRaw(genesisPath)
226230
require.NoError(t, err)
227231

228232
genTrie, err := genesis.NewTrieFromGenesis(gen)
@@ -272,7 +276,8 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) {
272276
}
273277

274278
func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) {
275-
gen, err := genesis.NewGenesisFromJSONRaw("../../../chain/polkadot/genesis.json")
279+
genesisPath := utils.GetPolkadotGenesisPath(t)
280+
gen, err := genesis.NewGenesisFromJSONRaw(genesisPath)
276281
require.NoError(t, err)
277282

278283
genTrie, err := genesis.NewTrieFromGenesis(gen)

0 commit comments

Comments
 (0)