-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
422 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package composer | ||
|
||
import ( | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
type ICmdFactory interface { | ||
MakeInstallCmd(command string, file string) (*exec.Cmd, error) | ||
} | ||
|
||
type IExecPath interface { | ||
LookPath(file string) (string, error) | ||
} | ||
|
||
type ExecPath struct { | ||
} | ||
|
||
func (ExecPath) LookPath(file string) (string, error) { | ||
return exec.LookPath(file) | ||
} | ||
|
||
type CmdFactory struct { | ||
execPath IExecPath | ||
} | ||
|
||
func (cmdf CmdFactory) MakeInstallCmd(command string, file string) (*exec.Cmd, error) { | ||
path, err := cmdf.execPath.LookPath(command) | ||
|
||
fileDir := filepath.Dir(file) | ||
|
||
return &exec.Cmd{ | ||
Path: path, | ||
Args: []string{command, "update", | ||
"--no-interaction", // We can't answer any prompts... | ||
"--no-scripts", // Avoid risky scripts | ||
"--ignore-platform-reqs", // We won't run the code, so we don't care about the platform | ||
"--no-autoloader", // We won't execute any code, no need for autoloader | ||
"--no-install", // No need to install packages | ||
"--no-plugins", // We won't run the code, so no plugins needed | ||
"--no-audit", // We don't want to run an audit | ||
}, | ||
Dir: fileDir, | ||
}, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package composer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMakeInstallCmd(t *testing.T) { | ||
composerCommand := "composer" | ||
cmd, err := CmdFactory{ | ||
execPath: ExecPath{}, | ||
}.MakeInstallCmd(composerCommand, "file") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, cmd) | ||
args := cmd.Args | ||
assert.Contains(t, args, "composer") | ||
assert.Contains(t, args, "update") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package composer | ||
|
||
import ( | ||
"github.com/debricked/cli/internal/resolution/job" | ||
) | ||
|
||
const ( | ||
composer = "composer" | ||
) | ||
|
||
type Job struct { | ||
job.BaseJob | ||
install bool | ||
composerCommand string | ||
cmdFactory ICmdFactory | ||
} | ||
|
||
func NewJob( | ||
file string, | ||
install bool, | ||
cmdFactory ICmdFactory, | ||
) *Job { | ||
return &Job{ | ||
BaseJob: job.NewBaseJob(file), | ||
install: install, | ||
cmdFactory: cmdFactory, | ||
} | ||
} | ||
|
||
func (j *Job) Install() bool { | ||
return j.install | ||
} | ||
|
||
func (j *Job) Run() { | ||
if j.install { | ||
|
||
j.SendStatus("installing dependencies") | ||
_, err := j.runInstallCmd() | ||
if err != nil { | ||
j.Errors().Critical(err) | ||
|
||
return | ||
} | ||
} | ||
|
||
} | ||
|
||
func (j *Job) runInstallCmd() ([]byte, error) { | ||
|
||
j.composerCommand = composer | ||
installCmd, err := j.cmdFactory.MakeInstallCmd(j.composerCommand, j.GetFile()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
installCmdOutput, err := installCmd.Output() | ||
if err != nil { | ||
return nil, j.GetExitError(err) | ||
} | ||
|
||
return installCmdOutput, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package composer | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
jobTestdata "github.com/debricked/cli/internal/resolution/job/testdata" | ||
"github.com/debricked/cli/internal/resolution/pm/composer/testdata" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
badName = "bad-name" | ||
) | ||
|
||
func TestNewJob(t *testing.T) { | ||
j := NewJob("file", false, CmdFactory{ | ||
execPath: ExecPath{}, | ||
}) | ||
assert.Equal(t, "file", j.GetFile()) | ||
assert.False(t, j.Errors().HasError()) | ||
} | ||
|
||
func TestRunInstall(t *testing.T) { | ||
cmdFactoryMock := testdata.NewEchoCmdFactory() | ||
j := NewJob("file", false, cmdFactoryMock) | ||
|
||
_, err := j.runInstallCmd() | ||
assert.NoError(t, err) | ||
|
||
assert.False(t, j.Errors().HasError()) | ||
} | ||
|
||
func TestInstall(t *testing.T) { | ||
j := Job{install: true} | ||
assert.Equal(t, true, j.Install()) | ||
|
||
j = Job{install: false} | ||
assert.Equal(t, false, j.Install()) | ||
} | ||
|
||
func TestRunInstallCmdErr(t *testing.T) { | ||
cmdErr := errors.New("cmd-error") | ||
cmdFactoryMock := testdata.NewEchoCmdFactory() | ||
cmdFactoryMock.MakeInstallErr = cmdErr | ||
j := NewJob("file", true, cmdFactoryMock) | ||
|
||
go jobTestdata.WaitStatus(j) | ||
j.Run() | ||
|
||
assert.Len(t, j.Errors().GetAll(), 1) | ||
assert.Contains(t, j.Errors().GetAll(), cmdErr) | ||
} | ||
|
||
func TestRunInstallCmdOutputErr(t *testing.T) { | ||
cmdMock := testdata.NewEchoCmdFactory() | ||
cmdMock.InstallCmdName = badName | ||
j := NewJob("file", true, cmdMock) | ||
|
||
go jobTestdata.WaitStatus(j) | ||
j.Run() | ||
|
||
jobTestdata.AssertPathErr(t, j.Errors()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package composer | ||
|
||
const Name = "composer" | ||
|
||
type Pm struct { | ||
name string | ||
} | ||
|
||
func NewPm() Pm { | ||
return Pm{ | ||
name: Name, | ||
} | ||
} | ||
|
||
func (pm Pm) Name() string { | ||
return pm.name | ||
} | ||
|
||
func (Pm) Manifests() []string { | ||
return []string{ | ||
`composer\.json$`, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package composer | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewPm(t *testing.T) { | ||
pm := NewPm() | ||
assert.Equal(t, Name, pm.name) | ||
} | ||
|
||
func TestName(t *testing.T) { | ||
pm := NewPm() | ||
assert.Equal(t, Name, pm.Name()) | ||
} | ||
|
||
func TestManifests(t *testing.T) { | ||
pm := Pm{} | ||
manifests := pm.Manifests() | ||
assert.Len(t, manifests, 1) | ||
manifest := manifests[0] | ||
assert.Equal(t, `composer\.json$`, manifest) | ||
_, err := regexp.Compile(manifest) | ||
assert.NoError(t, err) | ||
|
||
cases := map[string]bool{ | ||
"composer.json": true, | ||
"composer.lock": false, | ||
"package-lock.json": false, | ||
} | ||
for file, isMatch := range cases { | ||
t.Run(file, func(t *testing.T) { | ||
matched, _ := regexp.MatchString(manifest, file) | ||
assert.Equal(t, isMatch, matched) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package composer | ||
|
||
import ( | ||
"github.com/debricked/cli/internal/resolution/job" | ||
) | ||
|
||
type Strategy struct { | ||
files []string | ||
} | ||
|
||
func (s Strategy) Invoke() ([]job.IJob, error) { | ||
var jobs []job.IJob | ||
for _, file := range s.files { | ||
jobs = append(jobs, NewJob( | ||
file, | ||
true, | ||
CmdFactory{ | ||
execPath: ExecPath{}, | ||
}, | ||
), | ||
) | ||
} | ||
|
||
return jobs, nil | ||
} | ||
|
||
func NewStrategy(files []string) Strategy { | ||
return Strategy{files} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package composer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewStrategy(t *testing.T) { | ||
s := NewStrategy(nil) | ||
assert.NotNil(t, s) | ||
assert.Len(t, s.files, 0) | ||
|
||
s = NewStrategy([]string{}) | ||
assert.NotNil(t, s) | ||
assert.Len(t, s.files, 0) | ||
|
||
s = NewStrategy([]string{"file"}) | ||
assert.NotNil(t, s) | ||
assert.Len(t, s.files, 1) | ||
|
||
s = NewStrategy([]string{"file-1", "file-2"}) | ||
assert.NotNil(t, s) | ||
assert.Len(t, s.files, 2) | ||
} | ||
|
||
func TestInvokeNoFiles(t *testing.T) { | ||
s := NewStrategy([]string{}) | ||
jobs, _ := s.Invoke() | ||
assert.Empty(t, jobs) | ||
} | ||
|
||
func TestInvokeOneFile(t *testing.T) { | ||
s := NewStrategy([]string{"file"}) | ||
jobs, _ := s.Invoke() | ||
assert.Len(t, jobs, 1) | ||
} | ||
|
||
func TestInvokeManyFiles(t *testing.T) { | ||
s := NewStrategy([]string{"file-1", "file-2"}) | ||
jobs, _ := s.Invoke() | ||
assert.Len(t, jobs, 2) | ||
} |
Oops, something went wrong.