Add Command tests and refactor this further
This commit is contained in:
parent
6eaef572d5
commit
e61816438b
|
@ -1,7 +1,46 @@
|
|||
package scan
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Args []string
|
||||
cmd *exec.Cmd
|
||||
}
|
||||
|
||||
func (command *Command) run() {
|
||||
command.cmd = exec.Command(command.Name, command.Args...)
|
||||
}
|
||||
|
||||
func (command *Command) Start() {
|
||||
command.cmd.Start()
|
||||
}
|
||||
|
||||
func (command *Command) Wait() {
|
||||
command.cmd.Wait()
|
||||
}
|
||||
|
||||
func (command *Command) NewScanner() *bufio.Scanner {
|
||||
command.run()
|
||||
|
||||
stderr, err := command.cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get stderr pipe: %v", err)
|
||||
}
|
||||
|
||||
stdout, err := command.cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get stdout pipe: %v", err)
|
||||
}
|
||||
merged := io.MultiReader(stderr, stdout)
|
||||
return bufio.NewScanner(merged)
|
||||
}
|
||||
|
||||
func (command *Command) ExitCode() int {
|
||||
return command.cmd.ProcessState.ExitCode()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package scan
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCommandRun(t *testing.T) {
|
||||
command := Command{Name: "echo", Args: []string{"Here", "be", "a", "test"}}
|
||||
|
||||
scanner := command.NewScanner()
|
||||
command.Start()
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if text != "Here be a test" {
|
||||
t.Error("Didn't do shit with" + text)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestCommandOutput(t *testing.T) {
|
||||
command := Command{Name: "go", Args: []string{"version"}}
|
||||
|
||||
scanner := command.NewScanner()
|
||||
command.Start()
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if !strings.HasPrefix(text, "go version") {
|
||||
t.Error("Didn't do shit with" + text)
|
||||
} else {
|
||||
t.Logf("Found correct output of %v", text)
|
||||
}
|
||||
}
|
||||
t.Logf("COMMAND %v", command.cmd.ProcessState.ExitCode())
|
||||
|
||||
// TODO: Find out why this is always -1
|
||||
// if command.ExitCode() != 0 {
|
||||
// t.Error("Didn't run successfully", command.ExitCode())
|
||||
// }
|
||||
}
|
||||
|
||||
func TestCommandFailure(t *testing.T) {
|
||||
command := Command{Name: "go", Args: []string{"gahdned"}}
|
||||
|
||||
scanner := command.NewScanner()
|
||||
command.Start()
|
||||
var text []string
|
||||
for scanner.Scan() {
|
||||
text = append(text, scanner.Text())
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(text[0], "go gahdned") {
|
||||
t.Error("Didn't do shit with" + text[0])
|
||||
}
|
||||
t.Logf("## Command Output %v", command.cmd.ProcessState.ExitCode())
|
||||
}
|
|
@ -1,12 +1,10 @@
|
|||
package scan
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
|
@ -31,32 +29,21 @@ func Process(basedir string, document *Document) string {
|
|||
commands = append(commands, Command{Name: "ocrmypdf", Args: []string{"--language=deu", filepath.Join(basedir, document.FullName()+"_input.pdf"), filepath.Join(basedir, document.FullName()+".pdf")}})
|
||||
|
||||
for _, command := range commands {
|
||||
cmd := exec.Command(command.Name, command.Args...)
|
||||
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get stderr pipe: %v", err)
|
||||
}
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get stdout pipe: %v", err)
|
||||
}
|
||||
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
document.Events <- command.Name + " Startet"
|
||||
merged := io.MultiReader(stderr, stdout)
|
||||
scanner := bufio.NewScanner(merged)
|
||||
|
||||
scanner := command.NewScanner()
|
||||
|
||||
for scanner.Scan() {
|
||||
document.Events <- scanner.Text()
|
||||
}
|
||||
document.Events <- command.Name + " Ist fast fertig"
|
||||
done <- true
|
||||
}()
|
||||
cmd.Start()
|
||||
command.Start()
|
||||
document.Events <- command.Name + " Ist gestartet"
|
||||
cmd.Wait()
|
||||
command.Wait()
|
||||
<-done
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue