Skip to content

Commit

Permalink
Merge pull request #5 from 0x19/unit-tests
Browse files Browse the repository at this point in the history
Cleanups
  • Loading branch information
0x19 authored Aug 20, 2023
2 parents d9446d7 + ec1b76a commit 78096f0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 90 deletions.
10 changes: 10 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ func TestCompiler(t *testing.T) {
solc: solc,
sync: false,
},
{
name: "Invalid Compiler config",
wantErr: true,
wantCompileErr: true,
compilerConfig: func() *CompilerConfig {
return nil
}(),
solc: solc,
sync: false,
},
}

for _, testCase := range testCases {
Expand Down
17 changes: 0 additions & 17 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ import (
)

// validatePath checks the validity of a given path.
// It ensures that:
// - The path exists.
// - The path points to a directory.
// - The directory is readable.
//
// Parameters:
// - path: The file system path to validate.
//
// Returns:
// - nil if the path is valid.
// - An error if the path is invalid or any other error occurs.
func validatePath(path string) error {
info, err := os.Stat(path)
if os.IsNotExist(err) {
Expand Down Expand Up @@ -46,12 +35,6 @@ func validatePath(path string) error {
}

// getCleanedVersionTag removes the "v" prefix from a version tag.
//
// Parameters:
// - versionTag: A string representing the version tag to be cleaned.
//
// Returns:
// - A string representing the cleaned version tag.
func getCleanedVersionTag(versionTag string) string {
return strings.ReplaceAll(versionTag, "v", "")
}
19 changes: 6 additions & 13 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@ import (

// GetProductionLogger creates and returns a new production logger using the zap library.
// The production logger is optimized for performance and is suitable for use in production environments.
//
// Returns:
// - A pointer to the created zap.Logger instance.
// - An error if there's any issue creating the logger.
func GetProductionLogger() (*zap.Logger, error) {
logger, err := zap.NewProduction()
// The log level can be set using the provided level parameter.
func GetProductionLogger(level zapcore.Level) (*zap.Logger, error) {
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(level)
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, err := config.Build()
return logger, err
}

// GetDevelopmentLogger creates and returns a new development logger using the zap library.
// The development logger is optimized for development and debugging, providing more detailed logs.
// The log level can be set using the provided level parameter.
//
// Parameters:
// - level: The desired log level (e.g., DebugLevel, InfoLevel, etc.).
//
// Returns:
// - A pointer to the created zap.Logger instance.
// - An error if there's any issue creating the logger.
func GetDevelopmentLogger(level zapcore.Level) (*zap.Logger, error) {
config := zap.NewDevelopmentConfig()
config.Level = zap.NewAtomicLevelAt(level)
Expand Down
28 changes: 0 additions & 28 deletions releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ func (s *Solc) GetLocalReleasesPath() string {
}

// GetLocalReleases fetches the Solidity versions saved locally in releases.json.
//
// Returns:
// - A slice of Version representing all the fetched Solidity versions.
// - An error if there's any issue during the fetch process.
func (s *Solc) GetLocalReleases() ([]Version, error) {
data, err := os.ReadFile(s.GetLocalReleasesPath())
if err != nil {
Expand All @@ -34,18 +30,11 @@ func (s *Solc) GetLocalReleases() ([]Version, error) {
}

// GetCachedReleases returns the cached releases from memory.
//
// Returns:
// - A slice of Version representing all the cached Solidity versions.
func (s *Solc) GetCachedReleases() []Version {
return s.localReleases
}

// GetLatestRelease reads the memory cache or local releases.json file and returns the latest Solidity version.
//
// Returns:
// - A pointer to the latest Version.
// - An error if there's any issue during the fetch process.
func (s *Solc) GetLatestRelease() (*Version, error) {
var versions []Version

Expand All @@ -69,13 +58,6 @@ func (s *Solc) GetLatestRelease() (*Version, error) {
}

// GetRelease reads the memory cache or local releases.json file and returns the Solidity version matching the given tag name.
//
// Parameters:
// - tagName: A string representing the tag name of the desired Solidity version.
//
// Returns:
// - A pointer to the matching Version.
// - An error if there's any issue during the fetch process or if the version is not found.
func (s *Solc) GetRelease(tagName string) (*Version, error) {
var versions []Version

Expand Down Expand Up @@ -107,10 +89,6 @@ func (s *Solc) GetRelease(tagName string) (*Version, error) {
}

// GetReleasesSimplified fetches the Solidity versions saved locally in releases.json and returns a simplified version info.
//
// Returns:
// - A slice of VersionInfo representing the simplified version information.
// - An error if there's any issue during the fetch process.
func (s *Solc) GetReleasesSimplified() ([]VersionInfo, error) {
var versions []Version

Expand Down Expand Up @@ -159,12 +137,6 @@ func (s *Solc) GetBinary(version string) (string, error) {
}

// RemoveBinary removes the binary file of the specified version.
//
// Parameters:
// - version: A string representing the Solidity version whose binary should be removed.
//
// Returns:
// - An error if there's any issue during the removal process or if the binary is not found.
func (s *Solc) RemoveBinary(version string) error {
version = getCleanedVersionTag(version)
_, err := s.GetRelease(version)
Expand Down
2 changes: 1 addition & 1 deletion releases/releases.json
Git LFS file not shown
2 changes: 1 addition & 1 deletion solc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestSolc(t *testing.T) {
logger, err := GetProductionLogger()
logger, err := GetProductionLogger(zap.InfoLevel)
assert.NoError(t, err)
assert.NotNil(t, logger)
zap.ReplaceGlobals(logger)
Expand Down
33 changes: 3 additions & 30 deletions syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (
)

// SyncReleases fetches the available Solidity versions from GitHub, saves them to releases.json, and reloads the local cache.
//
// Returns:
// - A slice of Version representing all the fetched Solidity versions.
// - An error if there's any issue during the synchronization process.
func (s *Solc) SyncReleases() ([]Version, error) {
var allVersions []Version
page := 1
Expand Down Expand Up @@ -87,13 +83,6 @@ func (s *Solc) SyncReleases() ([]Version, error) {
}

// SyncBinaries downloads all the binaries for the specified versions in parallel.
//
// Parameters:
// - versions: A slice of Version representing the Solidity versions for which binaries should be downloaded.
// - limitVersion: A string representing a specific version to limit the download to. If empty, binaries for all versions will be downloaded.
//
// Returns:
// - An error if there's any issue during the download process.
func (s *Solc) SyncBinaries(versions []Version, limitVersion string) error {
var wg sync.WaitGroup
errorsCh := make(chan error, len(versions))
Expand All @@ -117,8 +106,8 @@ func (s *Solc) SyncBinaries(versions []Version, limitVersion string) error {

if _, err := os.Stat(filename); os.IsNotExist(err) {
totalDownloads++
zap.L().Debug(
"Downloading solc release",
zap.L().Info(
"Downloading missing solc release",
zap.String("version", versionTag),
zap.String("asset_name", asset.Name),
zap.String("asset_local_filename", filepath.Base(filename)),
Expand Down Expand Up @@ -193,9 +182,6 @@ func (s *Solc) SyncBinaries(versions []Version, limitVersion string) error {

// Sync fetches the available Solidity versions from GitHub, saves them to releases.json, reloads the local cache,
// and downloads all the binaries for the distribution for future use.
//
// Returns:
// - An error if there's any issue during the synchronization process.
func (s *Solc) Sync() error {
versions, err := s.SyncReleases()
if err != nil {
Expand All @@ -213,12 +199,6 @@ func (s *Solc) Sync() error {

// SyncOne fetches a specific Solidity version from GitHub, saves it to releases.json, reloads the local cache,
// and downloads the binary for the distribution for future use.
//
// Parameters:
// - version: A pointer to a Version representing the specific Solidity version to be synchronized.
//
// Returns:
// - An error if there's any issue during the synchronization process.
func (s *Solc) SyncOne(version *Version) error {
if version == nil {
return fmt.Errorf("version must be provided to synchronize one version")
Expand All @@ -242,13 +222,6 @@ func (s *Solc) SyncOne(version *Version) error {
}

// downloadFile downloads a file from the provided URL and saves it to the specified path.
//
// Parameters:
// - filepath: A string representing the path where the downloaded file should be saved.
// - url: A string representing the URL from which the file should be downloaded.
//
// Returns:
// - An error if there's any issue during the download process.
func (s *Solc) downloadFile(file string, url string) error {
// Just a bit of the time because we could receive 503 from GitHub so we don't want to spam them
randomDelayBetween500And1500()
Expand Down Expand Up @@ -284,7 +257,7 @@ func (s *Solc) downloadFile(file string, url string) error {
// #nosec G302
// G302 (CWE-276): Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM)
// We want executable files to be executable by the user running the program so we can't use 0600.
if err := os.Chmod(file, 0777); err != nil {
if err := os.Chmod(file, 0700); err != nil {
return fmt.Errorf("failed to set file as executable: %v", err)
}

Expand Down

0 comments on commit 78096f0

Please sign in to comment.