Skip to content

Commit

Permalink
Ups missing functionality for sources that are requred for compiler t…
Browse files Browse the repository at this point in the history
…o work
  • Loading branch information
0x19 committed Aug 17, 2023
1 parent bb25c8d commit 7ee3c5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ func (s *Sources) TruncateDir(path string) error {
return nil
}

// GetSolidityVersion extracts the Solidity version from the entry source unit.
func (s *Sources) GetSolidityVersion() (string, error) {
// Get the entry source unit by its name
entrySourceUnit := s.GetSourceUnitByName(s.EntrySourceUnitName)
if entrySourceUnit == nil {
return "", fmt.Errorf("entry source unit not found")
}

// Use a regular expression to match the pragma solidity statement
// This regex will match versions like ^0.x.x and extract only 0.x.x
re := regexp.MustCompile(`pragma solidity\s*\^?(\d+\.\d+\.\d+);`)
match := re.FindStringSubmatch(entrySourceUnit.Content)
if len(match) < 2 {
return "", fmt.Errorf("solidity version not found in entry source unit")
}

return match[1], nil
}

// handleImports extracts import statements from the source unit and adds them to the sources.
func (s *Sources) handleImports(sourceUnit *SourceUnit) ([]*SourceUnit, error) {
imports := extractImports(sourceUnit.Content)
Expand Down
9 changes: 9 additions & 0 deletions sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestSources(t *testing.T) {
sources *Sources
expected string
expectedUnits int
noSourceUnit bool
}{
{
name: "Test Case 1",
Expand All @@ -37,6 +38,7 @@ func TestSources(t *testing.T) {
},
expected: "Content of Source 1\n\nContent of Source 2",
expectedUnits: 2,
noSourceUnit: true,
},
{
name: "Openzeppelin import",
Expand All @@ -53,6 +55,7 @@ func TestSources(t *testing.T) {
},
expected: tests.ReadContractFileForTestFromRootPath(t, "contracts/cheelee/Combined").Content,
expectedUnits: 15,
noSourceUnit: true,
},
{
name: "OpenZeppelin ERC20 Test",
Expand Down Expand Up @@ -115,6 +118,12 @@ func TestSources(t *testing.T) {
assert.True(t, testCase.sources.SourceUnitExists(testCase.sources.SourceUnits[0].Name))
assert.NotNil(t, testCase.sources.GetSourceUnitByName(testCase.sources.SourceUnits[0].Name))
assert.NotNil(t, testCase.sources.GetSourceUnitByPath(testCase.sources.SourceUnits[0].Path))

if !testCase.noSourceUnit {
version, err := testCase.sources.GetSolidityVersion()
assert.NoError(t, err)
assert.NotEmpty(t, version)
}
})
}
}
Expand Down

0 comments on commit 7ee3c5b

Please sign in to comment.