diff --git a/.travis.yml b/.travis.yml index 2e3480a..a05fbbd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,10 @@ sudo: required - +language: go +install: + # Add Godeps dependencies to GOPATH and PATH + - export GOPATH="${TRAVIS_BUILD_DIR}/Godeps/_workspace:$GOPATH" + - export PATH="${TRAVIS_BUILD_DIR}/Godeps/_workspace/bin:$PATH" services: - docker - script: - make test diff --git a/Makefile b/Makefile index 87249d9..12230de 100644 --- a/Makefile +++ b/Makefile @@ -25,9 +25,8 @@ ifneq (${DOCKER},) endif .PHONY: test -test: test-plugin +test: clean test-plugin -# TODO: add unit tests test-unit: go test ${TEST_FLAGS} -coverprofile=coverage diff --git a/project.go b/project.go index 2951aa1..c0ba435 100644 --- a/project.go +++ b/project.go @@ -7,6 +7,13 @@ import ( "strings" ) +func Min(x, y int) int { + if x < y { + return x + } + return y +} + // https://github.com/go-yaml/yaml/issues/100 type StringArray []string @@ -54,11 +61,8 @@ func (p *Project) checkAffected(changedFiles []string) bool { normalizedPath := path.Clean(filePath) projectDirs := strings.Split(normalizedPath, "/") for _, changedFile := range changedFiles { - if changedFile == "" { - continue - } changedDirs := strings.Split(changedFile, "/") - if reflect.DeepEqual(changedDirs[:len(projectDirs)], projectDirs) { + if reflect.DeepEqual(changedDirs[:Min(len(projectDirs), len(changedDirs))], projectDirs) { return true } } diff --git a/project_test.go b/project_test.go new file mode 100644 index 0000000..8a88703 --- /dev/null +++ b/project_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCheckAffected(t *testing.T) { + assert := assert.New(t) + + changedFiles := []string{"project1/app.py", "project2/README.md", "", "README.md"} + + p1 := Project{Label: "project1", Path: []string{"project1/"}, Skip: []string{}} + assert.Equal(true, p1.checkAffected(changedFiles)) + + p2 := Project{Label: "project2", Path: []string{"project2"}, Skip: []string{"somelabel"}} + assert.Equal(true, p2.checkAffected(changedFiles)) + + p3 := Project{Label: "project3", Path: []string{"project3/", "project2/foo/"}, Skip: []string{"project1"}} + assert.Equal(false, p3.checkAffected(changedFiles)) + + // test no changes + assert.Equal(false, p3.checkAffected([]string{})) + +}