File: sh_test.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (90 lines) | stat: -rw-r--r-- 1,961 bytes parent folder | download
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
// Copyright 2012 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package sh

import (
	"errors"
	"testing"
)

var testsOk = []struct {
	cmd   string
	match bool
}{
	// expansion of "~"
	{"ls ~/", true},
}

var testsOutput = []struct {
	cmd   string
	out   string
	match bool
}{
	// values in match
	{"true", "", true},
	{"false", "", false},
	{`grep foo not_exist.go`, "", false},         // no found
	{`grep package sh.go`, "package sh\n", true}, // found

	// pipes
	{"ls sh*.go | wc -l", "2\n", true},

	// quotes
	{`sh -c 'echo 123'`, "123\n", true},
	{`sh -c "echo 123"`, "123\n", true},
	{`find -name 'sh*.go'`, "./sh.go\n./sh_test.go\n", true},
}

var testsError = []struct {
	cmd string
	err error // from Stderr
}{
	{"| ls ", errNoCmdInPipe},
	{"| ls | wc", errNoCmdInPipe},
	{"ls|", errNoCmdInPipe},
	{"ls| wc|", errNoCmdInPipe},
	{"ls| |wc", errNoCmdInPipe},

	{"LANG= C find", errEnvVar},
	{"LANG =C find", errEnvVar},

	{`LANG=C find -nop README.md`, errors.New("find: unknown predicate `-nop'")},
}

func TestRun(t *testing.T) {
	for _, v := range testsOk {
		out, match, _ := RunWithMatch(v.cmd)

		if v.match != match {
			t.Errorf("`%s` (match): expected %t, found %t\n", v.cmd, v.match, match)
		}

		if string(out) == "" {
			t.Errorf("`%s`: output is empty", v.cmd)
		}
	}

	for _, v := range testsOutput {
		out, match, _ := RunWithMatch(v.cmd)

		if string(out) != v.out {
			t.Errorf("`%s` (output): expected %q, found %q\n", v.cmd, v.out, out)
		}
		if match != v.match {
			t.Errorf("`%s` (match): expected %t, found %t\n", v.cmd, v.match, match)
		}
	}

	for _, v := range testsError {
		_, err := Run(v.cmd)
		mainErr := err.(runError).err

		if mainErr.Error() != v.err.Error() {
			t.Errorf("`%s` (error): expected %q, found %q\n", v.cmd, v.err, mainErr)
		}
	}
}