| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package python | 5 package python |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "os" | 9 "os" |
| 10 "os/exec" | 10 "os/exec" |
| 11 "testing" | 11 "testing" |
| 12 | 12 |
| 13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 14 | 14 |
| 15 . "github.com/luci/luci-go/common/testing/assertions" | 15 . "github.com/luci/luci-go/common/testing/assertions" |
| 16 . "github.com/smartystreets/goconvey/convey" | 16 . "github.com/smartystreets/goconvey/convey" |
| 17 ) | 17 ) |
| 18 | 18 |
| 19 func TestParsePythonCommandLine(t *testing.T) { | 19 func TestParsePythonCommandLine(t *testing.T) { |
| 20 t.Parallel() | 20 t.Parallel() |
| 21 | 21 |
| 22 successes := []struct { | 22 successes := []struct { |
| 23 args []string | 23 args []string |
| 24 cmd CommandLine | 24 cmd CommandLine |
| 25 }{ | 25 }{ |
| 26 » » {nil, CommandLine{}}, | 26 » » {nil, CommandLine{Target: NoTarget{}}}, |
| 27 | 27 |
| 28 {[]string{"-a", "-b", "-Q'foo.bar.baz'", "-Wbar"}, | 28 {[]string{"-a", "-b", "-Q'foo.bar.baz'", "-Wbar"}, |
| 29 CommandLine{ | 29 CommandLine{ |
| 30 » » » » Flags: []string{"-a", "-b", "-Q'foo.bar.baz'", "
-Wbar"}, | 30 » » » » Target: NoTarget{}, |
| 31 » » » » Args: []string{}, | 31 » » » » Flags: []string{"-a", "-b", "-Q'foo.bar.baz'",
"-Wbar"}, |
| 32 » » » » Args: []string{}, |
| 32 }, | 33 }, |
| 33 }, | 34 }, |
| 34 | 35 |
| 35 {[]string{"path.py", "--", "foo", "bar"}, | 36 {[]string{"path.py", "--", "foo", "bar"}, |
| 36 CommandLine{ | 37 CommandLine{ |
| 37 » » » » Type: TargetScript, | 38 » » » » Target: ScriptTarget{"path.py"}, |
| 38 » » » » Value: "path.py", | 39 » » » » Flags: []string{}, |
| 39 » » » » Flags: []string{}, | 40 » » » » Args: []string{"--", "foo", "bar"}, |
| 40 » » » » Args: []string{"--", "foo", "bar"}, | |
| 41 }, | 41 }, |
| 42 }, | 42 }, |
| 43 | 43 |
| 44 {[]string{"-a", "-Wfoo", "-", "--", "foo"}, | 44 {[]string{"-a", "-Wfoo", "-", "--", "foo"}, |
| 45 CommandLine{ | 45 CommandLine{ |
| 46 » » » » Type: TargetScript, | 46 » » » » Target: ScriptTarget{"-"}, |
| 47 » » » » Value: "-", | 47 » » » » Flags: []string{"-a", "-Wfoo"}, |
| 48 » » » » Flags: []string{"-a", "-Wfoo"}, | 48 » » » » Args: []string{"--", "foo"}, |
| 49 » » » » Args: []string{"--", "foo"}, | |
| 50 }, | 49 }, |
| 51 }, | 50 }, |
| 52 | 51 |
| 53 {[]string{"-a", "-b", "-W", "foo", "-Wbar", "-c", "<script>", "-
-", "arg"}, | 52 {[]string{"-a", "-b", "-W", "foo", "-Wbar", "-c", "<script>", "-
-", "arg"}, |
| 54 CommandLine{ | 53 CommandLine{ |
| 55 » » » » Type: TargetCommand, | 54 » » » » Target: CommandTarget{"<script>"}, |
| 56 » » » » Value: "<script>", | 55 » » » » Flags: []string{"-a", "-b", "-W", "foo", "-Wbar
"}, |
| 57 » » » » Flags: []string{"-a", "-b", "-W", "foo", "-Wbar"
}, | 56 » » » » Args: []string{"--", "arg"}, |
| 58 » » » » Args: []string{"--", "arg"}, | |
| 59 }, | 57 }, |
| 60 }, | 58 }, |
| 61 | 59 |
| 62 {[]string{"-a", "-b", "-m'foo.bar.baz'", "arg"}, | 60 {[]string{"-a", "-b", "-m'foo.bar.baz'", "arg"}, |
| 63 CommandLine{ | 61 CommandLine{ |
| 64 » » » » Type: TargetModule, | 62 » » » » Target: ModuleTarget{"'foo.bar.baz'"}, |
| 65 » » » » Value: "'foo.bar.baz'", | 63 » » » » Flags: []string{"-a", "-b"}, |
| 66 » » » » Flags: []string{"-a", "-b"}, | 64 » » » » Args: []string{"arg"}, |
| 67 » » » » Args: []string{"arg"}, | |
| 68 }, | 65 }, |
| 69 }, | 66 }, |
| 70 } | 67 } |
| 71 | 68 |
| 72 failures := []struct { | 69 failures := []struct { |
| 73 args []string | 70 args []string |
| 74 err string | 71 err string |
| 75 }{ | 72 }{ |
| 76 {[]string{"-a", "-b", "-Q"}, "truncated two-variable argument"}, | 73 {[]string{"-a", "-b", "-Q"}, "truncated two-variable argument"}, |
| 77 {[]string{"-c"}, "missing second value"}, | 74 {[]string{"-c"}, "missing second value"}, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 164 |
| 168 So(cmd.Run(c, "foo", "bar"), ShouldBeNil
) | 165 So(cmd.Run(c, "foo", "bar"), ShouldBeNil
) |
| 169 So(lastCmd, ShouldResemble, &exec.Cmd{ | 166 So(lastCmd, ShouldResemble, &exec.Cmd{ |
| 170 Path: "/path/to/python", | 167 Path: "/path/to/python", |
| 171 Args: []string{"/path/to/pytho
n", "-B", "-E", "-s", "foo", "bar"}, | 168 Args: []string{"/path/to/pytho
n", "-B", "-E", "-s", "foo", "bar"}, |
| 172 Stdout: os.Stdout, | 169 Stdout: os.Stdout, |
| 173 Stderr: os.Stderr, | 170 Stderr: os.Stderr, |
| 174 }) | 171 }) |
| 175 }) | 172 }) |
| 176 | 173 |
| 177 Convey(`Can connect STDIN.`, func() { | |
| 178 cmd.ConnectSTDIN = true | |
| 179 | |
| 180 So(cmd.Run(c, "foo", "bar"), ShouldBeNil
) | |
| 181 So(lastCmd, ShouldResemble, &exec.Cmd{ | |
| 182 Path: "/path/to/python", | |
| 183 Args: []string{"/path/to/pytho
n", "foo", "bar"}, | |
| 184 Stdout: os.Stdout, | |
| 185 Stderr: os.Stderr, | |
| 186 Stdin: os.Stdin, | |
| 187 }) | |
| 188 }) | |
| 189 | |
| 190 Convey(`Will forward a working directory and env
iornment`, func() { | 174 Convey(`Will forward a working directory and env
iornment`, func() { |
| 191 cmd.WorkDir = "zugzug" | 175 cmd.WorkDir = "zugzug" |
| 192 cmd.Env = []string{"pants=on"} | 176 cmd.Env = []string{"pants=on"} |
| 193 | 177 |
| 194 So(cmd.Run(c, "foo", "bar"), ShouldBeNil
) | 178 So(cmd.Run(c, "foo", "bar"), ShouldBeNil
) |
| 195 So(lastCmd, ShouldResemble, &exec.Cmd{ | 179 So(lastCmd, ShouldResemble, &exec.Cmd{ |
| 196 Path: "/path/to/python", | 180 Path: "/path/to/python", |
| 197 Args: []string{"/path/to/pytho
n", "foo", "bar"}, | 181 Args: []string{"/path/to/pytho
n", "foo", "bar"}, |
| 198 Stdout: os.Stdout, | 182 Stdout: os.Stdout, |
| 199 Stderr: os.Stderr, | 183 Stderr: os.Stderr, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 217 Convey(fmt.Sprintf(`Will fail to parse %
q (%s)`, tc.output, tc.err), func() { | 201 Convey(fmt.Sprintf(`Will fail to parse %
q (%s)`, tc.output, tc.err), func() { |
| 218 runnerOutput = tc.output | 202 runnerOutput = tc.output |
| 219 _, err := i.GetVersion(c) | 203 _, err := i.GetVersion(c) |
| 220 So(err, ShouldErrLike, tc.err) | 204 So(err, ShouldErrLike, tc.err) |
| 221 }) | 205 }) |
| 222 } | 206 } |
| 223 }) | 207 }) |
| 224 }) | 208 }) |
| 225 }) | 209 }) |
| 226 } | 210 } |
| OLD | NEW |