Skip to content

Commit

Permalink
Merge pull request #22 from hourglasshoro/bugfix/21_relative_path
Browse files Browse the repository at this point in the history
Fixed a bug that prevented you from specifying a path with al…
  • Loading branch information
0x-shanks authored Feb 1, 2021
2 parents 96366d1 + e17e52f commit a577ba8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
13 changes: 5 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import (
"fmt"
"github.com/hourglasshoro/graphmize/pkg/file"
"github.com/hourglasshoro/graphmize/pkg/graph"
"github.com/hourglasshoro/graphmize/pkg/imput"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"os"
"path"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"os"
)

var cfgFile string
Expand All @@ -50,10 +49,8 @@ You can open a dashboard in your browser and see a graph of dependencies represe
if err != nil {
return errors.Wrap(err, "cannot get current dir")
}
if source != "" {
currentDir = path.Join(currentDir, source)
}
graph, err := graph.BuildGraph(*ctx, currentDir)
graphDir := imput.Solve(source, currentDir)
graph, err := graph.BuildGraph(*ctx, graphDir)
if err != nil {
return errors.Wrap(err, "cannot build graph")
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/imput/solve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package imput

import (
"path"
"path/filepath"
)

// Solve resolves the path of the root directory to be searched from the input source and currentDir
func Solve(source string, currentDir string) (graphDir string) {
if source != "" && !filepath.IsAbs(source) {
graphDir = path.Join(currentDir, source)
} else if filepath.IsAbs(source) {
graphDir = source
} else {
graphDir = currentDir
}
return
}
32 changes: 32 additions & 0 deletions pkg/imput/solve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package imput

import (
"github.com/stretchr/testify/assert"
"testing"
)

// Solve Test

// TestSolveWithNoSource tests to validate when not specified in the -s option
func TestSolveWithNoSource(t *testing.T) {
source := ""
actual := Solve(source, "/User/test//app")
expected := "/User/test//app"
assert.Equal(t, expected, actual)
}

// TestSolveWithNoSource tests to validate when a relative path is specified with the -s option
func TestSolveWithRelativePath(t *testing.T) {
source := "../app2"
actual := Solve(source, "/User/test/app1")
expected := "/User/test/app2"
assert.Equal(t, expected, actual)
}

// TestSolveWithNoSource tests to validate when a absolute path is specified with the -s option
func TestSolveWithAbsolutePath(t *testing.T) {
source := "/User/test/app2"
actual := Solve(source, "/app1")
expected := "/User/test/app2"
assert.Equal(t, expected, actual)
}

0 comments on commit a577ba8

Please sign in to comment.