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

Revert "Remove formula tmp dir (#875)" #886

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ package cmd

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

"github.com/stretchr/testify/assert"
Expand All @@ -40,7 +37,6 @@ 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 @@ -340,15 +336,7 @@ func TestCreateFormula(t *testing.T) {
assert.DirExists(t, filepath.Join(reposDir, "local-default"))
assert.FileExists(t, filepath.Join(reposDir, "local-default", "tree.json"))

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(hashesDir, "-tmp-.ritchie-formulas-local-test-test.txt"))

assert.FileExists(t, filepath.Join(reposDir, "repositories.json"))
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ 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 @@ -154,10 +156,10 @@ func Build() *cobra.Command {
inputResolver := runner.NewInputResolver(termInputTypes)

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

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

runners := formula.Runners{
formula.LocalRun: formulaLocalRun,
Expand Down
8 changes: 0 additions & 8 deletions pkg/formula/builder/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,15 @@ 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: 0 additions & 3 deletions pkg/formula/builder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ 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: 9 additions & 0 deletions pkg/formula/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"runtime"
"strings"

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

"github.com/ZupIT/ritchie-cli/pkg/api"
Expand Down Expand Up @@ -104,6 +105,7 @@ type (
FormulaPath string
BinName string
BinPath string
TmpDir string
Config Config
ContainerId string
}
Expand Down Expand Up @@ -145,6 +147,13 @@ 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: 7 additions & 0 deletions pkg/formula/formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ 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: 27 additions & 5 deletions pkg/formula/runner/docker/pre_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

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

Expand Down Expand Up @@ -78,7 +79,6 @@ 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,11 +88,13 @@ 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(binPath); err != nil {
if err := pr.dir.Remove(def.BinPath(formulaPath)); err != nil {
return formula.Setup{}, err
}

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

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

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

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

dockerFile := filepath.Join(binPath, "Dockerfile")
dockerFile := filepath.Join(tmpDir, "Dockerfile")
if !pr.file.Exists(dockerFile) {
return formula.Setup{}, ErrDockerfileNotFound
}
Expand Down Expand Up @@ -158,6 +166,20 @@ 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: 39 additions & 11 deletions pkg/formula/runner/docker/pre_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ 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 @@ -100,7 +99,7 @@ func TestPreRun(t *testing.T) {
out: out{
want: formula.Setup{},
wantErr: true,
err: errors.New("failed building formula with Docker, we will try to build your formula locally"),
err: nil,
},
},
{
Expand Down Expand Up @@ -164,6 +163,32 @@ 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 @@ -173,11 +198,19 @@ func TestPreRun(t *testing.T) {
preRun := NewPreRun(ritHome, in.dockerBuild, in.dir, in.file)
got, err := preRun.PreRun(in.def)

if err != nil || tt.out.err != nil {
assert.EqualError(t, tt.out.err, err.Error())
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)
}
}

assert.Equal(t, tt.out.want.Config, got.Config)
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)
}

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

Expand All @@ -239,10 +271,6 @@ 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