forked from natefinch/sh
/
sh_test.go
141 lines (114 loc) · 3.1 KB
/
sh_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sh_test
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/natefinch/sh"
)
const SWCrawl = `
A long time ago, in a galaxy far, far away....
It is a period of civil war. Rebel
spaceships, striking from a hidden
base, have won their first victory
against the evil Galactic Empire.
During the battle, Rebel spies managed
to steal secret plans to the Empire's
ultimate weapon, the Death Star, an
armored space station with enough
power to destroy an entire planet.
Pursued by the Empire's sinister agents,
Princess Leia races home aboard her
starship, custodian of the stolen plans
that can save her people and restore
freedom to the galaxy...`
func ExampleCmd() {
echo := sh.Cmd("echo")
fmt.Print(echo("Hi there!"))
// output:
// Hi there!
}
func ExampleRunner() {
echo := sh.Runner("echo")
// functions created with runner call the underlying shell command
// immediately and return its standard output and an error if any.
out, err := echo("Hi there!")
fmt.Print(out)
fmt.Print(err)
// output:
// Hi there!
// <nil>
}
func ExamplePipe() {
echo := sh.Cmd("echo")
// note, you can "bake in" arguments when you create these functions.
upper := sh.Cmd("tr", "[:lower:]", "[:upper:]")
grep := sh.Cmd("grep")
// Equivalent of shell command:
// $ echo Hi there! | grep -o Hi | wc -w
fmt.Print(sh.Pipe(echo(SWCrawl), grep("far"), upper()))
// output:
// A LONG TIME AGO, IN A GALAXY FAR, FAR AWAY....
}
func ExamplePipeWith() {
upper := sh.Cmd("tr", "[:lower:]", "[:upper:]")
grep := sh.Cmd("grep")
fmt.Print(sh.PipeWith(SWCrawl, grep("far"), upper()))
// output:
// A LONG TIME AGO, IN A GALAXY FAR, FAR AWAY....
}
func Example_String() {
echo := sh.Cmd("echo")
executable := echo("Hi there!")
// Since we're passing the output into a function expecting a string, we
// have to call String() on the output of the command.
var s string = executable.String()
fmt.Print(s)
// output:
// Hi there!
}
func ExampleDump() {
grep := sh.Cmd("grep")
name := "ExampleDumpTest"
defer writeTempFile(name, SWCrawl)()
// Equivalent of shell command
// $ cat ExampleDumpTest | grep far
fmt.Print(sh.Pipe(sh.Dump(name), grep("far")))
// output:
// A long time ago, in a galaxy far, far away....
}
func ExampleRead() {
grep := sh.Cmd("grep")
name := "ExampleReadTest"
f, cleanup := openTempFile(name, SWCrawl)
defer cleanup()
fmt.Print(sh.Pipe(sh.Read(f), grep("far")))
// output:
// A long time ago, in a galaxy far, far away....
}
func TestString(t *testing.T) {
ex := sh.Cmd("thiswontwork")()
s := ex.String()
expected := "exec: \"thiswontwork\": executable file not found in $PATH"
if s != expected {
t.Errorf("expected %q, got %q", expected, s)
}
}
func openTempFile(name, content string) (f *os.File, cleanup func()) {
err := ioutil.WriteFile(name, []byte(content), 0777)
if err != nil {
panic(err)
}
f, err = os.Open(name)
if err != nil {
panic(err)
}
return f, func() { f.Close(); os.Remove(name) }
}
func writeTempFile(name, content string) (cleanup func()) {
err := ioutil.WriteFile(name, []byte(content), 0777)
if err != nil {
panic(err)
}
return func() { os.Remove(name) }
}