Skip to content

Commit

Permalink
fix unit tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
sagerb committed Dec 17, 2024
1 parent 3b57cb7 commit 9be66c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
10 changes: 10 additions & 0 deletions internal/initialize/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"github.com/posit-dev/publisher/internal/config"
"github.com/posit-dev/publisher/internal/inspect"
"github.com/posit-dev/publisher/internal/inspect/detectors"
"github.com/posit-dev/publisher/internal/interpreters"
"github.com/posit-dev/publisher/internal/logging"
"github.com/posit-dev/publisher/internal/util"
"github.com/posit-dev/publisher/internal/util/utiltest"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

Expand All @@ -34,6 +36,14 @@ func (s *InitializeSuite) SetupTest() {
s.cwd = cwd
err = cwd.MkdirAll(0700)
s.NoError(err)

interpreters.NewRInterpreter = func(baseDir util.AbsolutePath, rExec util.Path, log logging.Logger) interpreters.RInterpreter {
i := interpreters.NewMockRInterpreter()
i.On("Init").Return(nil)
i.On("RequiresR", mock.Anything).Return(false, nil)
i.On("GetLockFilePath").Return(util.RelativePath{}, false, nil)
return i
}
}

func (s *InitializeSuite) TestInitEmpty() {
Expand Down
10 changes: 10 additions & 0 deletions internal/inspect/r_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package inspect
import (
"testing"

"github.com/posit-dev/publisher/internal/interpreters"
"github.com/posit-dev/publisher/internal/logging"
"github.com/posit-dev/publisher/internal/util"
"github.com/posit-dev/publisher/internal/util/utiltest"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

Expand All @@ -27,6 +29,14 @@ func (s *RSuite) SetupTest() {
s.cwd = cwd
err = cwd.MkdirAll(0700)
s.NoError(err)

interpreters.NewRInterpreter = func(baseDir util.AbsolutePath, rExec util.Path, log logging.Logger) interpreters.RInterpreter {
i := interpreters.NewMockRInterpreter()
i.On("Init").Return(nil)
i.On("RequiresR", mock.Anything).Return(false, nil)
i.On("GetLockFilePath").Return(util.RelativePath{}, false, nil)
return i
}
}

func (s *RSuite) TestNewRInspector() {
Expand Down
8 changes: 4 additions & 4 deletions internal/interpreters/r_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ func (m *MockRInterpreter) GetLockFilePath() (util.RelativePath, bool, error) {
arg0 := args.Get(0)
arg1 := args.Get(1)
if arg0 == nil {
return util.RelativePath{}, false, args.Error(1)
return util.RelativePath{}, false, args.Error(2)
} else {
var iPath interface{} = arg0
path, ok := iPath.(string)
path, ok := iPath.(util.RelativePath)
if !ok {
path = ""
path = util.RelativePath{}
}
var iExists interface{} = arg1
exists, ok := iExists.(bool)
if !ok {
exists = false
}
return util.NewRelativePath(path, nil), exists, args.Error(1)
return util.NewRelativePath(path.String(), nil), exists, args.Error(2)
}
}

Expand Down
19 changes: 11 additions & 8 deletions internal/interpreters/r_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import (
type RSuite struct {
utiltest.Suite
cwd util.AbsolutePath
fs afero.Fs
}

func TestRSuite(t *testing.T) {
suite.Run(t, new(RSuite))
}

func (s *RSuite) SetupTest() {
cwd, err := util.Getwd(afero.NewMemMapFs())
s.fs = afero.NewMemMapFs()
cwd, err := util.Getwd(s.fs)
s.NoError(err)
s.cwd = cwd
err = cwd.MkdirAll(0700)
Expand All @@ -37,7 +39,7 @@ func (s *RSuite) SetupTest() {

func (s *RSuite) TestNewRInterpreter() {
log := logging.New()
rPath := util.NewPath("/usr/bin/R", nil)
rPath := util.NewPath("/usr/bin/R", s.fs)
i := NewRInterpreter(s.cwd, rPath, log)
interpreter := i.(*defaultRInterpreter)
s.Equal(rPath, interpreter.preferredPath)
Expand Down Expand Up @@ -174,6 +176,7 @@ func (s *RSuite) TestGetRVersionFromExecutable() {
rPath := s.cwd.Join("bin", "R")
rPath.Dir().MkdirAll(0777)
rPath.WriteFile(nil, 0777)

rInterpreter := NewRInterpreter(s.cwd, rPath.Path, log)
executor := executortest.NewMockExecutor()
executor.On("RunCommand", mock.Anything, []string{"--version"}, mock.Anything, mock.Anything).Return([]byte(tc.versionOutput), nil, nil)
Expand Down Expand Up @@ -238,14 +241,14 @@ type RExecutableValidTestData struct {
expectedIsRExecutableValidResult bool
}

func getRExecutableValidTestData() []RExecutableValidTestData {
func getRExecutableValidTestData(fs afero.Fs) []RExecutableValidTestData {
data := []RExecutableValidTestData{
{initialized: false, rExecutable: util.AbsolutePath{}, version: "", expectedIsRExecutableValidResult: false},
{initialized: false, rExecutable: util.NewAbsolutePath("abc", nil), version: "", expectedIsRExecutableValidResult: false},
{initialized: false, rExecutable: util.NewAbsolutePath("abc", fs), version: "", expectedIsRExecutableValidResult: false},
{initialized: false, rExecutable: util.AbsolutePath{}, version: "1.2.3", expectedIsRExecutableValidResult: false},
{initialized: true, rExecutable: util.NewAbsolutePath("abc", nil), version: "", expectedIsRExecutableValidResult: false},
{initialized: true, rExecutable: util.NewAbsolutePath("abc", fs), version: "", expectedIsRExecutableValidResult: false},
{initialized: true, rExecutable: util.AbsolutePath{}, version: "1.2.3", expectedIsRExecutableValidResult: false},
{initialized: true, rExecutable: util.NewAbsolutePath("abc", nil), version: "1.2.3", expectedIsRExecutableValidResult: true},
{initialized: true, rExecutable: util.NewAbsolutePath("abc", fs), version: "1.2.3", expectedIsRExecutableValidResult: true},
}
return data
}
Expand All @@ -261,7 +264,7 @@ func (s *RSuite) TestIsRExecutableValid() {

interpreter.initialized = true
interpreter.rExecutable = util.AbsolutePath{}
for _, tc := range getRExecutableValidTestData() {
for _, tc := range getRExecutableValidTestData(s.fs) {
interpreter.initialized = tc.initialized
interpreter.rExecutable = tc.rExecutable
interpreter.version = tc.version
Expand Down Expand Up @@ -373,7 +376,7 @@ func (s *RSuite) TestResolveRExecutableWhenPathContainsRButNotValid() {

err := interpreter.resolveRExecutable()
s.Error(err)
s.Equal("Unable to detect any R interpreters", err.Error())
s.Equal("unable to detect any R interpreters", err.Error())
}

// Validate that we find the lock file that R specifies
Expand Down

0 comments on commit 9be66c0

Please sign in to comment.