Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Remove formula tmp dir (#875)
Browse files Browse the repository at this point in the history
* fixing tests

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix tests

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix lint

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix tests

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Remove TmpWordDirPath function

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix formula builders

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix lint

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix variable name

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix run formula on Windows

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>

* Fix lint

Signed-off-by: Kadu Artur Prussek <kadu.artur@gmail.com>
  • Loading branch information
kaduartur authored Mar 10, 2021
1 parent d9cc458 commit 0ca725c
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 587 deletions.
14 changes: 13 additions & 1 deletion pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package cmd

import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -37,6 +40,7 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula/workspace"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/git/gitlab"
"github.com/ZupIT/ritchie-cli/pkg/os/osutil"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)
Expand Down Expand Up @@ -336,7 +340,15 @@ func TestCreateFormula(t *testing.T) {
assert.DirExists(t, filepath.Join(reposDir, "local-default"))
assert.FileExists(t, filepath.Join(reposDir, "local-default", "tree.json"))

assert.FileExists(t, filepath.Join(hashesDir, "-tmp-.ritchie-formulas-local-test-test.txt"))
if osutil.Darwin == runtime.GOOS {
tmpDir := strings.ReplaceAll(os.TempDir(), "/", "-")
hashFile := fmt.Sprintf("%s.ritchie-formulas-local-test-test.txt", tmpDir)
assert.FileExists(t, filepath.Join(hashesDir, hashFile))
}

if osutil.Linux == runtime.GOOS {
assert.FileExists(t, filepath.Join(hashesDir, "-tmp-.ritchie-formulas-local-test-test.txt"))
}

assert.FileExists(t, filepath.Join(reposDir, "repositories.json"))
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ func Build() *cobra.Command {
formBuildDocker := builder.NewBuildDocker(fileManager)
formBuildLocal := builder.NewBuildLocal(ritchieHomeDir, dirManager, repoAdder)

postRunner := runner.NewPostRunner(fileManager, dirManager)

promptInManager := fprompt.NewInputManager(credResolver, inputList, inputText, inputTextValidator, inputTextDefault, inputBool, inputPassword, inputMultiselect, inputAutocomplete)
stdinInManager := stdin.NewInputManager(credResolver)
flagInManager := flag.NewInputManager(credResolver)
Expand All @@ -156,10 +154,10 @@ func Build() *cobra.Command {
inputResolver := runner.NewInputResolver(termInputTypes)

formulaLocalPreRun := local.NewPreRun(ritchieHomeDir, formBuildMake, formBuildBat, formBuildSh, dirManager, fileManager)
formulaLocalRun := local.NewRunner(postRunner, inputResolver, formulaLocalPreRun, fileManager, envFinder, userHomeDir)
formulaLocalRun := local.NewRunner(userHomeDir, fileManager, envFinder, inputResolver, formulaLocalPreRun)

formulaDockerPreRun := docker.NewPreRun(ritchieHomeDir, formBuildDocker, dirManager, fileManager)
formulaDockerRun := docker.NewRunner(postRunner, inputResolver, formulaDockerPreRun, fileManager, envFinder, userHomeDir)
formulaDockerRun := docker.NewRunner(userHomeDir, inputResolver, formulaDockerPreRun, envFinder)

runners := formula.Runners{
formula.LocalRun: formulaLocalRun,
Expand Down
8 changes: 8 additions & 0 deletions pkg/formula/builder/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ func NewBuildMake() MakeManager {
}

func (ma MakeManager) Build(info formula.BuildInfo) error {
pwd, _ := os.Getwd()
defer os.Chdir(pwd) //nolint:errcheck

if err := os.Chdir(info.FormulaPath); err != nil {
return err
}

var stderr bytes.Buffer
cmd := exec.Command("make", "build")
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf(errMsgFmt, ErrBuildFormulaMakefile, stderr.String())
}

if err := os.Chdir(pwd); err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions pkg/formula/builder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func NewBuildShell() ShellManager {
}

func (sh ShellManager) Build(info formula.BuildInfo) error {
pwd, _ := os.Getwd()
defer os.Chdir(pwd) //nolint:errcheck

if err := os.Chdir(info.FormulaPath); err != nil {
return err
}
Expand Down
9 changes: 0 additions & 9 deletions pkg/formula/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"runtime"
"strings"

"github.com/google/uuid"
"github.com/spf13/pflag"

"github.com/ZupIT/ritchie-cli/pkg/api"
Expand Down Expand Up @@ -105,7 +104,6 @@ type (
FormulaPath string
BinName string
BinPath string
TmpDir string
Config Config
ContainerId string
}
Expand Down Expand Up @@ -147,13 +145,6 @@ func (d *Definition) FormulaPath(home string) string {
return filepath.Join(home, ReposDir, d.RepoName, d.Path)
}

// TmpWorkDirPath builds the tmp paths to run formula, first parameter is tmpDir created
// second parameter is tmpBinDir
func (d *Definition) TmpWorkDirPath(home string) string {
u := uuid.New().String()
return filepath.Join(home, TmpDir, u)
}

func (d *Definition) UnixBinFilePath(fPath string) string {
return filepath.Join(fPath, BinDir, BinUnix)
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/formula/formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ func TestFormulaPath(t *testing.T) {
assert.Equal(t, want, got)
}

func TestTmpWorkDirPath(t *testing.T) {
want := filepath.Join(home, TmpDir)
gotTmpDir := def.TmpWorkDirPath(home)

assert.Contains(t, gotTmpDir, want)
}

func TestBinPath(t *testing.T) {
want := filepath.Join(home, "repos", "commons", "scaffold", "coffee-java", "bin")
formulaPath := def.FormulaPath(home)
Expand Down
32 changes: 5 additions & 27 deletions pkg/formula/runner/docker/pre_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/kaduartur/go-cli-spinner/pkg/spinner"

Expand Down Expand Up @@ -79,6 +78,7 @@ func NewPreRun(
func (pr PreRunManager) PreRun(def formula.Definition) (formula.Setup, error) {
pwd, _ := os.Getwd()
formulaPath := def.FormulaPath(pr.ritchieHome)
binPath := def.BinPath(formulaPath)

config, err := pr.loadConfig(formulaPath, def)
if err != nil {
Expand All @@ -88,13 +88,11 @@ func (pr PreRunManager) PreRun(def formula.Definition) (formula.Setup, error) {
binFilePath := def.UnixBinFilePath(formulaPath)
if !pr.file.Exists(binFilePath) {
s := spinner.StartNew("Building formula...")
time.Sleep(2 * time.Second)

if err := pr.buildFormula(formulaPath, config.DockerIB); err != nil {
s.Stop()

// Remove /bin dir to force formula rebuild in next execution
if err := pr.dir.Remove(def.BinPath(formulaPath)); err != nil {
if err := pr.dir.Remove(binPath); err != nil {
return formula.Setup{}, err
}

Expand All @@ -104,25 +102,19 @@ func (pr PreRunManager) PreRun(def formula.Definition) (formula.Setup, error) {
s.Success(prompt.Green("Formula was successfully built!"))
}

tmpDir, err := pr.createWorkDir(pr.ritchieHome, formulaPath, def)
if err != nil {
return formula.Setup{}, err
}

if err := os.Chdir(tmpDir); err != nil {
if err := os.Chdir(binPath); err != nil {
return formula.Setup{}, err
}

s := formula.Setup{
Pwd: pwd,
FormulaPath: formulaPath,
BinName: def.BinName(),
BinPath: def.BinPath(formulaPath),
TmpDir: tmpDir,
BinPath: binPath,
Config: config,
}

dockerFile := filepath.Join(tmpDir, "Dockerfile")
dockerFile := filepath.Join(binPath, "Dockerfile")
if !pr.file.Exists(dockerFile) {
return formula.Setup{}, ErrDockerfileNotFound
}
Expand Down Expand Up @@ -166,20 +158,6 @@ func (pr PreRunManager) loadConfig(formulaPath string, def formula.Definition) (
return formulaConfig, nil
}

func (pr PreRunManager) createWorkDir(home, formulaPath string, def formula.Definition) (string, error) {
tDir := def.TmpWorkDirPath(home)
if err := pr.dir.Create(tDir); err != nil {
return "", err
}

binPath := def.BinPath(formulaPath)
if err := pr.dir.Copy(binPath, tDir); err != nil {
return "", err
}

return tDir, nil
}

func buildRunImg(def formula.Definition) (string, error) {
prompt.Info("Docker image build started")
formName := strings.ReplaceAll(def.Path, string(os.PathSeparator), "-")
Expand Down
50 changes: 11 additions & 39 deletions pkg/formula/runner/docker/pre_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/builder"
"github.com/ZupIT/ritchie-cli/pkg/stream"
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestPreRun(t *testing.T) {
out: out{
want: formula.Setup{},
wantErr: true,
err: nil,
err: errors.New("failed building formula with Docker, we will try to build your formula locally"),
},
},
{
Expand Down Expand Up @@ -163,32 +164,6 @@ func TestPreRun(t *testing.T) {
err: errors.New("invalid character 'e' looking for beginning of value"),
},
},
{
name: "create work dir error",
in: in{
def: formula.Definition{Path: "testing/formula", RepoName: "commons"},
dockerBuild: dockerBuilder,
file: fileManager,
dir: dirManagerMock{createErr: errors.New("error to create dir")},
},
out: out{
wantErr: true,
err: errors.New("error to create dir"),
},
},
{
name: "copy work dir error",
in: in{
def: formula.Definition{Path: "testing/formula", RepoName: "commons"},
dockerBuild: dockerBuilder,
file: fileManager,
dir: dirManagerMock{copyErr: errors.New("error to copy dir")},
},
out: out{
wantErr: true,
err: errors.New("error to copy dir"),
},
},
}

for _, tt := range tests {
Expand All @@ -198,19 +173,11 @@ func TestPreRun(t *testing.T) {
preRun := NewPreRun(ritHome, in.dockerBuild, in.dir, in.file)
got, err := preRun.PreRun(in.def)

if tt.out.wantErr {
if tt.out.err == nil && err == nil {
t.Errorf("PreRun(%s) want a error", tt.name)
}

if tt.out.err != nil && err != nil && tt.out.err.Error() != err.Error() {
t.Errorf("PreRun(%s) got %v, want %v", tt.name, err, tt.out.err)
}
if err != nil || tt.out.err != nil {
assert.EqualError(t, tt.out.err, err.Error())
}

if !reflect.DeepEqual(tt.out.want.Config, got.Config) {
t.Errorf("PreRun(%s) got %v, want %v", tt.name, got.Config, tt.out.want.Config)
}
assert.Equal(t, tt.out.want.Config, got.Config)

_ = os.Chdir(got.Pwd) // Return to test folder
})
Expand Down Expand Up @@ -252,6 +219,7 @@ type fileManagerMock struct {
rErr error
wErr error
aErr error
reErr error
exist bool
}

Expand All @@ -271,6 +239,10 @@ func (fi fileManagerMock) Append(path string, content []byte) error {
return fi.aErr
}

func (fi fileManagerMock) Remove(path string) error {
return fi.reErr
}

const (
configJson = `{
"dockerImageBuilder": "cimg/go:1.14",
Expand Down
Loading

0 comments on commit 0ca725c

Please sign in to comment.