Skip to content

Commit

Permalink
implement dry-run and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Jun 30, 2016
1 parent fe3438c commit 4eed929
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gargs_race
binaries
ssshtest
gargs
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: go

os:
- linux
- osx

go:
- 1.4
- 1.5
- 1.6

script:
- ./tests/functional-test.sh

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Usage
=====

```
usage: gargs [--procs PROCS] [--nlines NLINES] [--sep SEP] [--shell SHELL] [--verbose] [--continueonerror] [--ordered] COMMAND
usage: gargs [--procs PROCS] [--nlines NLINES] [--sep SEP] [--shell SHELL] [--verbose] [--continue-on-error] [--ordered] [--dry-run] COMMAND
positional arguments:
command command to execute
Expand All @@ -75,9 +75,10 @@ options:
--sep SEP, -s SEP regular expression split line with to fill multiple template spots default is not to split. -s and -n are mutually exclusive.
--shell SHELL shell to use [default: bash]
--verbose, -v print commands to stderr before they are executed.
--continueonerror, -c
--continue-on-error, -c
report errors but don't stop the entire execution (which is the default).
--ordered, -o keep output in order of input; default is to output in order of return which greatly improves parallelization.
--dry-run, -d print (but do not run) the commands (for debugging)
--help, -h display this help and exit
```

Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/brentp/xopen"
)

const VERSION = "0.2.0"
const VERSION = "0.3.0"

type Args struct {
Procs int `arg:"-p,help:number of processes to use"`
Expand All @@ -27,8 +27,9 @@ type Args struct {
Sep string `arg:"-s,help:regular expression split line with to fill multiple template spots default is not to split. -s and -n are mutually exclusive."`
Shell string `arg:"help:shell to use"`
Verbose bool `arg:"-v,help:print commands to stderr before they are executed."`
ContinueOnError bool `arg:"-c,help:report errors but don't stop the entire execution (which is the default)."`
ContinueOnError bool `arg:"-c,--continue-on-error,help:report errors but don't stop the entire execution (which is the default)."`
Ordered bool `arg:"-o,help:keep output in order of input; default is to output in order of return which greatly improves parallelization."`
DryRun bool `arg:"-d,--dry-run,help:print (but do not run) the commands"`
}

// hold the arguments for each call.
Expand All @@ -47,6 +48,7 @@ func main() {
args.Verbose = false
args.ContinueOnError = false
args.Ordered = false
args.DryRun = false
p := arg.MustParse(&args)
if args.Sep != "" && args.Nlines > 1 {
p.Fail("must specify either sep (-s) or n-lines (-n), not both")
Expand Down Expand Up @@ -190,6 +192,10 @@ func process(ch chan []byte, cmdStr string, args Args, xarg *xargs) {
if args.Verbose {
fmt.Fprintf(os.Stderr, "command: %s\n", cmdStr)
}
if args.DryRun {
fmt.Fprintf(os.Stdout, "%s\n", cmdStr)
return
}

cmd := exec.Command(args.Shell, "-c", cmdStr)
cmd.Stderr = os.Stderr
Expand Down
28 changes: 28 additions & 0 deletions tests/functional-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

test -e ssshtest || wget -q https://raw.githubusercontent.com/ryanlayer/ssshtest/master/ssshtest

. ssshtest

go build -o gargs_race -race -a

fn_check_basic() {
seq 12 -1 1 | ./gargs_race -p 5 -n 3 -d -v 'sleep {0}; echo {1} {2}'
}
run check_basic fn_check_basic
assert_exit_code 0
assert_in_stderr 'command:'
assert_equal 4 $(wc -l $STDOUT_FILE)
assert_equal 4 $(grep -c sleep $STDOUT_FILE)

fn_check_sep() {
cat tests/t.txt | ./gargs_race --sep "\s+" -p 2 "echo -e '{0}:{1}-{2}' full-line: \'{}\'"
}
run check_sep fn_check_sep
assert_exit_code 0
assert_in_stdout "chr2:22-33 full-line: 'chr2 22 33'"
assert_in_stdout "chr1:22-33 full-line: 'chr1 22 33'"
assert_in_stdout "chr3:22-33 full-line: 'chr3 22 33'"
assert_in_stdout "chr4:22-33 full-line: 'chr4 22 33'"


4 changes: 4 additions & 0 deletions tests/t.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
chr1 22 33
chr2 22 33
chr3 22 33
chr4 22 33

0 comments on commit 4eed929

Please sign in to comment.