Skip to content

Commit

Permalink
added tests to resolve module aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Sep 18, 2019
1 parent a28c2f4 commit 6048024
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
6 changes: 1 addition & 5 deletions evaluator/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,11 +1594,7 @@ func requireFn(tok token.Token, env *object.Environment, args ...object.Object)
packageAliasesLoaded = true
}

a, error := util.UnaliasPath(args[0].Inspect(), packageAliases)
if error != nil {
return newError(tok, "error resolving '%s': %s\n", args[0].Inspect(), error.Error())
}

a := util.UnaliasPath(args[0].Inspect(), packageAliases)
file := filepath.Join(env.Dir, a)
e := object.NewEnvironment(env.Writer, filepath.Dir(file))
return doSource(tok, e, file, args...)
Expand Down
8 changes: 4 additions & 4 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func UniqueStrings(slice []string) []string {

// UnaliasPath translates a path alias
// to the full path in the filesystem.
func UnaliasPath(path string, packageAlias map[string]string) (string, error) {
func UnaliasPath(path string, packageAlias map[string]string) string {
// An alias can come in different forms:
// - package
// - package/file.abs
Expand All @@ -113,7 +113,7 @@ func UnaliasPath(path string, packageAlias map[string]string) (string, error) {
parts := strings.Split(path, string(os.PathSeparator))

if len(parts) < 1 {
return path, nil
return path
}

if packageAlias[parts[0]] != "" {
Expand All @@ -130,7 +130,7 @@ func UnaliasPath(path string, packageAlias map[string]string) (string, error) {
p = append(p, "index.abs")
}

return filepath.Join(p...), nil
return filepath.Join(p...)
}
return path, nil
return path
}
27 changes: 27 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package util

import (
"testing"
)

func TestUnaliasPath(t *testing.T) {
tests := []struct {
path string
aliases map[string]string
expected string
}{
{"test", map[string]string{}, "test"},
{"test/sample.abs", map[string]string{}, "test/sample.abs"},
{"test/sample.abs", map[string]string{"test": "path"}, "path/sample.abs"},
{"test", map[string]string{"test": "path"}, "path/index.abs"},
{"./test", map[string]string{"test": "path"}, "./test"},
}

for _, tt := range tests {
res := UnaliasPath(tt.path, tt.aliases)

if res != tt.expected {
t.Fatalf("error unaliasing path, expected %s, got %s", tt.expected, res)
}
}
}

0 comments on commit 6048024

Please sign in to comment.