Skip to content

Commit

Permalink
Move validation of git input into config
Browse files Browse the repository at this point in the history
  • Loading branch information
chainchad committed Dec 6, 2024
1 parent 1d9497a commit cbda2d1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
20 changes: 12 additions & 8 deletions tools/gomod-local-update/internal/updater/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,24 @@ func (c *Config) Validate() error {
return nil
}

if c.OrgName == "" {
return fmt.Errorf("%w: org name must be provided", ErrInvalidConfig)
}
if c.RepoName == "" {
return fmt.Errorf("%w: repo name must be provided", ErrInvalidConfig)
}
if c.RepoRemote == "" {
return fmt.Errorf("%w: repo remote cannot be empty", ErrInvalidConfig)
return fmt.Errorf("%w: repo remote must be provided", ErrInvalidConfig)
}

if c.BranchTrunk == "" {
return fmt.Errorf("%w: branch trunk cannot be empty", ErrInvalidConfig)
return fmt.Errorf("%w: trunk branch must be provided", ErrInvalidConfig)
}

if c.OrgName == "" {
return fmt.Errorf("%w: organization name cannot be empty", ErrInvalidConfig)
if !gitRemoteRE.MatchString(c.RepoRemote) {
return fmt.Errorf("%w: git remote '%s' contains invalid characters", ErrInvalidConfig, c.RepoRemote)
}

if c.RepoName == "" {
return fmt.Errorf("%w: repository name cannot be empty", ErrInvalidConfig)
if !gitBranchRE.MatchString(c.BranchTrunk) {
return fmt.Errorf("%w: git branch '%s' contains invalid characters", ErrInvalidConfig, c.BranchTrunk)
}

return nil
Expand Down
44 changes: 43 additions & 1 deletion tools/gomod-local-update/internal/updater/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package updater

import "testing"
import (
"errors"
"testing"
)

func TestConfig_Validate(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -61,6 +64,26 @@ func TestConfig_Validate(t *testing.T) {
},
wantErr: true,
},
{
name: "invalid remote characters",
config: &Config{
OrgName: "test",
RepoName: "test",
RepoRemote: "origin!@#",
BranchTrunk: "main",
},
wantErr: true,
},
{
name: "invalid branch characters",
config: &Config{
OrgName: "test",
RepoName: "test",
RepoRemote: "origin",
BranchTrunk: "main!@#",
},
wantErr: true,
},
}

for _, tt := range tests {
Expand All @@ -73,6 +96,25 @@ func TestConfig_Validate(t *testing.T) {
}
}

func TestConfig_ValidateErrorType(t *testing.T) {
cfg := &Config{
RepoRemote: "invalid*remote",
BranchTrunk: "develop",
OrgName: "test",
RepoName: "test",
}

err := cfg.Validate()
if err == nil {
t.Error("expected error due to invalid repo remote, got nil")
return
}

if !errors.Is(err, ErrInvalidConfig) {
t.Errorf("expected error to be ErrInvalidConfig, got: %v", err)
}
}

func TestParseFlags(t *testing.T) {
tests := []struct {
name string
Expand Down
16 changes: 0 additions & 16 deletions tools/gomod-local-update/internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ func New(config *Config, system SystemOperator) *Updater {
}
}

// validateGitInput checks if the remote and branch are in the correct format
func (u *Updater) validateGitInput(remote, branch string) error {
if !gitRemoteRE.MatchString(remote) {
return fmt.Errorf("%w: git remote '%s' contains invalid characters", ErrInvalidConfig, remote)
}

if !gitBranchRE.MatchString(branch) {
return fmt.Errorf("%w: git branch '%s' contains invalid characters", ErrInvalidConfig, branch)
}
return nil
}

// validateSHA checks if the SHA consists of exactly 40 hexadecimal digits
func (u *Updater) validateSHA(sha string) error {
if !gitShaRE.MatchString(sha) {
Expand All @@ -87,10 +75,6 @@ func (u *Updater) validateSHA(sha string) error {

// getGitInfo retrieves the latest commit SHA and timestamp from a Git repository
func (u *Updater) getGitInfo(remote, branch string) (string, time.Time, error) {
if err := u.validateGitInput(remote, branch); err != nil {
return "", time.Time{}, err
}

ctx, cancel := context.WithTimeout(context.Background(), gitTimeout)
defer cancel()

Expand Down
31 changes: 0 additions & 31 deletions tools/gomod-local-update/internal/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package updater

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -286,36 +285,6 @@ replace github.com/smartcontractkit/chainlink/v3 => ../
}
}

func TestUpdater_Run_InvalidGitInput(t *testing.T) {
cfg := &Config{
RepoRemote: "invalid*remote",
BranchTrunk: "develop",
OrgName: "smartcontractkit",
RepoName: "chainlink",
}
sysOp := newMockSystemOperator()
sysOp.files["go.mod"] = []byte(`module test
require github.com/smartcontractkit/chainlink/v2 v2.0.0
replace github.com/smartcontractkit/chainlink/v2 => ../
`)

u := New(cfg, sysOp)
u.git = &mockGitExecutor{
sha: "ac7a7395feed" + strings.Repeat("0", 28),
time: time.Now(),
}
err := u.Run()
if err == nil {
t.Errorf("expected error due to invalid repo remote, got nil")
return
}

// Use errors.Is instead of errors.As since ErrInvalidConfig is a sentinel error
if !errors.Is(err, ErrInvalidConfig) {
t.Errorf("expected error to be ErrInvalidConfig, got: %v", err)
}
}

func TestUpdater_FindLocalReplaceModules(t *testing.T) {
sysOp := newMockSystemOperator()
sysOp.files["go.mod"] = []byte(`
Expand Down

0 comments on commit cbda2d1

Please sign in to comment.