-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils_test.go
More file actions
67 lines (55 loc) · 1.98 KB
/
utils_test.go
File metadata and controls
67 lines (55 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package sourcify
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"os"
"path/filepath"
"strconv"
)
// LoadContract loads a contract response from a JSON file in the testdata directory.
// The filename format is chainid:address.json
func LoadContract(chainID int, address common.Address) (*ContractResponse, error) {
// Create the filename pattern
filename := fmt.Sprintf("%d:%s.json", chainID, address.Hex())
// Find the testdata directory
testdataPath := filepath.Join("testdata", filename)
// Read the file
fileData, err := os.ReadFile(testdataPath)
if err != nil {
return nil, fmt.Errorf("failed to read test data file: %w", err)
}
// Unmarshal the JSON data
var contractResponse ContractResponse
if err := json.Unmarshal(fileData, &contractResponse); err != nil {
return nil, fmt.Errorf("failed to unmarshal contract data: %w", err)
}
return &contractResponse, nil
}
// SaveContract saves a contract response to a JSON file in the testdata directory.
// This is useful for creating test data from actual API responses.
func SaveContract(contractResponse *ContractResponse) error {
// Make sure the testdata directory exists
testdataPath := "testdata"
if err := os.MkdirAll(testdataPath, 0755); err != nil {
return fmt.Errorf("failed to create testdata directory: %w", err)
}
// Convert chainID to int
chainID, err := strconv.Atoi(contractResponse.ChainID)
if err != nil {
return fmt.Errorf("failed to convert chainID to int: %w", err)
}
// Create the filename pattern
filename := fmt.Sprintf("%d:%s.json", chainID, contractResponse.Address)
filePath := filepath.Join(testdataPath, filename)
// Marshal the contract response to JSON
data, err := json.MarshalIndent(contractResponse, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal contract data: %w", err)
}
// Write the file
if err := os.WriteFile(filePath, data, 0644); err != nil {
return fmt.Errorf("failed to write test data file: %w", err)
}
return nil
}