Skip to content

Commit

Permalink
Fix slices bounds error (#60)
Browse files Browse the repository at this point in the history
* Fix slices bounds error

* Fix formatting

* Update .travis.yml

* fixup! Update .travis.yml

* Add go deps

* Remove test-unit from test
  • Loading branch information
ksindi authored Apr 28, 2020
1 parent 456ee33 commit 527fca1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
Expand Down
26 changes: 26 additions & 0 deletions project_test.go
Original file line number Diff line number Diff line change
@@ -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{}))

}

0 comments on commit 527fca1

Please sign in to comment.