Skip to content

Commit

Permalink
Merge pull request #158 from apex/fix-loading
Browse files Browse the repository at this point in the history
fix loading functions (load only existing functions)
  • Loading branch information
tj committed Jan 27, 2016
2 parents 8c93398 + 2d9cbd1 commit 8f89884
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
Empty file.
34 changes: 15 additions & 19 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const (
DefaultTimeout = 3
)

// ErrNotFound is returned when a function cannot be found.
var ErrNotFound = errors.New("project: no function found")

// Config for project.
type Config struct {
Name string `json:"name" validate:"nonzero"`
Expand Down Expand Up @@ -119,15 +116,21 @@ func (p *Project) LoadFunctions(names ...string) error {
dir := filepath.Join(p.Path, functionsDir)
p.Log.Debugf("loading functions in %s", dir)

var err error
existing, err := p.FunctionDirNames()
if err != nil {
return err
}

if len(names) == 0 {
names, err = p.FunctionDirNames()
if err != nil {
return err
}
names = existing
}

for _, name := range names {
if !utils.ContainsString(existing, name) {
p.Log.Warnf("function %q does not exist in project", name)
continue
}

fn, err := p.loadFunction(name)
if err != nil {
return err
Expand All @@ -136,6 +139,10 @@ func (p *Project) LoadFunctions(names ...string) error {
p.Functions = append(p.Functions, fn)
}

if len(p.Functions) == 0 {
return errors.New("no function loaded")
}

return nil
}

Expand Down Expand Up @@ -213,17 +220,6 @@ func (p *Project) Delete() error {
return nil
}

// FunctionByName returns a function by `name` or returns ErrNotFound.
func (p *Project) FunctionByName(name string) (*function.Function, error) {
for _, fn := range p.Functions {
if fn.Name == name {
return fn, nil
}
}

return nil, ErrNotFound
}

// FunctionDirNames returns a list of function directory names.
func (p *Project) FunctionDirNames() (list []string, err error) {
dir := filepath.Join(p.Path, functionsDir)
Expand Down
25 changes: 25 additions & 0 deletions project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,28 @@ func TestProject_LoadFunctions_loadSpecified(t *testing.T) {
assert.Equal(t, 1, len(p.Functions))
assert.Equal(t, "foo", p.Functions[0].Name)
}

func TestProject_LoadFunctions_onlyExisting(t *testing.T) {
p := &project.Project{
Path: "_fixtures/twoFunctions",
Log: log.Log,
}

p.Open()
p.LoadFunctions("foo", "buz")

assert.Equal(t, 1, len(p.Functions))
assert.Equal(t, "foo", p.Functions[0].Name)
}

func TestProject_LoadFunctions_noFunctionLoaded(t *testing.T) {
p := &project.Project{
Path: "_fixtures/twoFunctions",
Log: log.Log,
}

p.Open()
err := p.LoadFunctions("buz")

assert.EqualError(t, err, "no function loaded")
}
10 changes: 10 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ func ReadIgnoreFile(dir string) ([]string, error) {

return strings.Split(string(b), "\n"), nil
}

// ContainsString checks if array contains string
func ContainsString(array []string, element string) bool {
for _, e := range array {
if element == e {
return true
}
}
return false
}
5 changes: 5 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ func Test_ReadIgnoreFile_missing(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []string(nil), patterns)
}

func Test_ContainsString(t *testing.T) {
assert.True(t, utils.ContainsString([]string{"a", "b"}, "a"))
assert.False(t, utils.ContainsString([]string{"a", "b"}, "c"))
}

0 comments on commit 8f89884

Please sign in to comment.