Skip to content

Commit

Permalink
Always run for changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilmat committed Sep 11, 2017
1 parent 38f7219 commit 722fd7f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .buildkite/publish.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION=$(cat VERSION)
VERSION=$(go run committer.go --version)
UPLOADED_PKG_FOUND=$(aws s3 --region 'us-west-2' ls 's3://vpc-access/' | grep committer-$VERSION)

if [ "$UPLOADED_PKG_FOUND" ]; then
Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

13 changes: 5 additions & 8 deletions committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import (
"flag"
"fmt"
"github.com/gusto/committer/core"
"io/ioutil"
"os"
)

func main() {
content, _ := ioutil.ReadFile("VERSION")
VERSION := string(content)
const VERSION = "0.1.3"

func main() {
version := flag.Bool("version", false, "Display version")
help := flag.Bool("help", false, "Display usage")
fix := flag.Bool("fix", false, "Run autocorrect for commands that support it")
changed := flag.Bool("changed", false, "Run autocorrect for commands that support it")
configPath := flag.String("config", "committer.yml", "Location of your config file")

flag.Parse()
Expand All @@ -27,16 +24,16 @@ func main() {
}

if *version {
fmt.Printf(VERSION)
fmt.Printf(VERSION + "\n")
return
}

parsedConfig, err := core.NewConfigFromFile(*configPath)
if err != nil {
return
panic(err)
}

success := core.NewRunner(*parsedConfig, *fix, *changed).Run()
success := core.NewRunner(*parsedConfig, *fix).Run()

if success {
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion configure.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash -ex

VERSION="0.1.2"
VERSION="0.1.3"
GIT_PRE_COMMIT_HOOK=".git/hooks/pre-commit"
COMMITTER_YML="committer.yml"
COMMITTER_LOCATION="/usr/local/bin/committer"
Expand Down
7 changes: 3 additions & 4 deletions core/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ type Runner struct {
resultChannel chan TaskResult
}

func NewRunner(config Config, fix bool, changed bool) *Runner {
func NewRunner(config Config, fix bool) *Runner {
return &Runner{
config: config,
fix: fix,
changed: changed,
resultChannel: make(chan TaskResult),
}
}
Expand All @@ -21,7 +20,7 @@ func (this Runner) Run() bool {

for i := 0; i < len(this.config.Tasks); i += 1 {
task := this.config.Tasks[i]
if task.shouldRun(this.changed) {
if task.shouldRun() {
tasksToRun = append(tasksToRun, task)
go this.processTask(task)
}
Expand All @@ -36,5 +35,5 @@ func (this Runner) Run() bool {
}

func (this Runner) processTask(task Task) {
this.resultChannel <- task.Execute(this.changed, this.fix)
this.resultChannel <- task.Execute(this.fix)
}
37 changes: 13 additions & 24 deletions core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ var execCommand = func(command string, args ...string) ([]byte, error) {
return exec.Command(command, args...).CombinedOutput()
}

func (task Task) Execute(changed bool, fix bool) TaskResult {
func (task Task) Execute(fix bool) TaskResult {
// Generate command based on --fix / --changed
command := task.prepareCommand(changed, fix)
command := task.prepareCommand(fix)

// Run command
output, err := execCommand(command[0], command[1:]...)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (task Task) prepareFixedOutput(outputStr string) string {
return strings.Join(fixedOutputList, "\n")
}

func (task Task) prepareCommand(changed bool, fix bool) []string {
func (task Task) prepareCommand(fix bool) []string {
// Use the FixCommand or regular Command depending on the flag passed to CLI
var cmdStr string
if fix && task.Fix.Command != "" {
Expand All @@ -107,11 +107,8 @@ func (task Task) prepareCommand(changed bool, fix bool) []string {
}

// Feed in changed files if we are running with --changed

if changed {
relevantChangedFilesList := task.relevantChangedFiles(changedFilesList)
cmdStr += " -- " + strings.Join(relevantChangedFilesList, " ")
}
relevantChangedFilesList := task.relevantChangedFiles(changedFilesList)
cmdStr += " " + strings.Join(relevantChangedFilesList, " ")

return strings.Split(cmdStr, " ")
}
Expand All @@ -125,24 +122,16 @@ func (task Task) stageRelevantFiles() {
}
}

func (task Task) shouldRun(changed bool) bool {
// Always run all tasks if we aren't just looking at changed files
if !changed {
return true
}

if task.Fix.Command != "" {
for _, file := range changedFilesList {
match, err := regexp.MatchString(task.Files, file)
func (task Task) shouldRun() bool {
for _, file := range changedFilesList {
match, err := regexp.MatchString(task.Files, file)

if err != nil {
panic(err)
}
if match {
return true
}
if err != nil {
panic(err)
}
if match {
return true
}
}

return false
}
31 changes: 11 additions & 20 deletions core/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@ import (
"testing"
)

func TestShouldRunNotChanged(t *testing.T) {
task := Task{
Name: "task",
Command: "run-task",
}

assert.True(t, task.shouldRun(false), "It runs when not in changed mode")
}

func TestShouldRunNotChangedNoFix(t *testing.T) {
task := Task{
Name: "task",
Command: "run-task",
}

assert.False(t, task.shouldRun(true), "It does not run when in changed mode with no fix command")
assert.False(t, task.shouldRun(), "It does not run when in changed mode with no fix command")
}

func TestShouldRunNotChangedWithFix(t *testing.T) {
Expand All @@ -32,7 +23,7 @@ func TestShouldRunNotChangedWithFix(t *testing.T) {
}
task.Fix.Command = "run-fix"

assert.True(t, task.shouldRun(true), "It does not run when in changed mode with no fix command")
assert.True(t, task.shouldRun(), "It does not run when in changed mode with no fix command")
}

func TestPrepareCommandNoChangeNoFix(t *testing.T) {
Expand All @@ -43,7 +34,7 @@ func TestPrepareCommandNoChangeNoFix(t *testing.T) {
assert.Equal(
t,
[]string{"run-task"},
task.prepareCommand(false, true),
task.prepareCommand(true),
"It runs the fix command when fix is true",
)
}
Expand All @@ -55,7 +46,7 @@ func TestPrepareCommandNoChangeFix(t *testing.T) {
assert.Equal(
t,
[]string{"run-fix"},
task.prepareCommand(false, true),
task.prepareCommand(true),
"It runs the fix command when fix is true",
)
}
Expand All @@ -72,7 +63,7 @@ func TestPrepareCommandWithChanged(t *testing.T) {
assert.Equal(
t,
[]string{"run-task", "--", "three.txt"},
task.prepareCommand(true, false),
task.prepareCommand(false),
"It correctly passes only the relevant files",
)
}
Expand Down Expand Up @@ -145,7 +136,7 @@ func TestExecuteSuccess(t *testing.T) {
Command: "run-task",
}

result := task.Execute(false, false)
result := task.Execute(false)
assert.True(t, result.success, "The result is successful")
assert.Equal(t, result.task, task, "It attaches the task")
assert.Equal(t, result.output, "Output!", "It attaches the task")
Expand All @@ -161,7 +152,7 @@ func TestExecuteFailure(t *testing.T) {
Command: "run-task",
}

result := task.Execute(false, false)
result := task.Execute(false)
assert.False(t, result.success, "The result is failed")
assert.Equal(t, result.task, task, "It attaches the task")
assert.Equal(t, result.output, "Output!", "It attaches the task")
Expand All @@ -177,7 +168,7 @@ func TestExecuteFixSuccessNoFixCommand(t *testing.T) {
Command: "run-task",
}

result := task.Execute(false, true)
result := task.Execute(true)
assert.True(t, result.success, "The result is successful")
assert.Equal(t, result.task, task, "It attaches the task")
assert.Equal(t, result.output, "Output!", "It does not grep through the output")
Expand All @@ -201,7 +192,7 @@ Linted: app/three.rb
task.Fix.Command = "run-fix"
task.Fix.Output = "Fixed:"

result := task.Execute(false, true)
result := task.Execute(true)
assert.True(t, result.success, "The result is successful")
assert.Equal(t, result.task, task, "It attaches the task")
assert.Equal(t, result.output, "Linted: app/one.rb\nFixed: app/two.rb\nLinted: app/three.rb\n", "It attaches the entire output")
Expand All @@ -222,7 +213,7 @@ func TestExecuteFixFailureWithFixCommand(t *testing.T) {
task.Fix.Command = "run-fix"
task.Fix.Output = "Fixed:"

result := task.Execute(false, true)
result := task.Execute(true)
assert.False(t, result.success, "The result is successful")
assert.Equal(t, result.task, task, "It attaches the task")
assert.Equal(t, result.output, "Failed!", "It attaches the entire output")
Expand Down Expand Up @@ -250,7 +241,7 @@ Linted: app/three.rb
task.Fix.Command = "run-fix"
task.Fix.Output = "Fixed:"

result := task.Execute(false, true)
result := task.Execute(true)
assert.False(t, result.success, "The result is marked unsuccessful so changes can be staged")
assert.Equal(t, result.task, task, "It attaches the task")
assert.Equal(t, result.output, "Linted: app/one.rb\nFixed: app/two.rb\nLinted: app/three.rb\n", "It attaches the entire output")
Expand Down

0 comments on commit 722fd7f

Please sign in to comment.