Skip to content

Commit 6f01a0b

Browse files
committed
[#200] Add vcs/p4 implementation for Add(), Commit(), Push()
1 parent ea48c51 commit 6f01a0b

10 files changed

+364
-32
lines changed

src/vcs/cmd/command.go

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (sc *ShellCommand) GetFullPath() string {
5353

5454
// Run calls the command with the provided parameters in a separate process and returns its output traces combined
5555
func (sc *ShellCommand) Run(params ...string) (output []byte, err error) {
56+
//report.PostWarning("Command: ", sc.name, " ", append(sc.params, params...))
5657
return sh.Command(sc.name, append(sc.params, params...)).CombinedOutput()
5758
}
5859

@@ -68,6 +69,7 @@ func (sc *ShellCommand) Trace(params ...string) error {
6869
// RunAndPipe calls the command with the provided parameters in a separate process
6970
// and pipes its output to cmd. Returns cmd's output traces combined
7071
func (sc *ShellCommand) RunAndPipe(cmd *ShellCommand, params ...string) (output []byte, err error) {
72+
//report.PostWarning("Command: ", sc.name, " ", append(sc.params, params...), " | ", cmd.name, " ", cmd.params)
7173
return sh.NewSession().
7274
Command(sc.name, append(sc.params, params...)).
7375
Command(cmd.name, cmd.params).

src/vcs/cmd/shell.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright (c) 2023 Murex
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
package cmd
24+
25+
import "golang.org/x/text/encoding/charmap"
26+
27+
// ShellAttributes contain shell-specific attributes allowing to tune behavior
28+
// when interacting with a shell command
29+
type ShellAttributes struct {
30+
Encoding *charmap.Charmap
31+
EOL string
32+
}
33+
34+
// GetShellAttributes Returns shell-specific attributes
35+
func GetShellAttributes() ShellAttributes {
36+
return osShellAttributes()
37+
}

src/vcs/cmd/shell_unix.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright (c) 2023 Murex
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package cmd
26+
27+
// osShellAttributes returns shell attributes associated to the underlying operating system
28+
func osShellAttributes() ShellAttributes {
29+
return ShellAttributes{
30+
Encoding: nil,
31+
EOL: "\n",
32+
}
33+
}

src/vcs/cmd/shell_unix_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright (c) 2023 Murex
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package cmd
26+
27+
import (
28+
"github.com/stretchr/testify/assert"
29+
"testing"
30+
)
31+
32+
func Test_unix_shell_encoding(t *testing.T) {
33+
// nil means that we use Go's default string encoding (UTF-8)
34+
// e.g. no conversion is required
35+
assert.Equal(t, nil, GetShellAttributes().Encoding)
36+
}
37+
38+
func Test_unix_shell_end_of_line(t *testing.T) {
39+
assert.Equal(t, "\n", GetShellAttributes().EOL)
40+
}

src/vcs/cmd/shell_windows.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright (c) 2023 Murex
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
package cmd
24+
25+
import "golang.org/x/text/encoding/charmap"
26+
27+
// osShellAttributes returns shell attributes associated to the underlying operating system
28+
func osShellAttributes() ShellAttributes {
29+
return ShellAttributes{
30+
Encoding: charmap.Windows1252,
31+
EOL: "\r\n",
32+
}
33+
}

src/vcs/cmd/shell_windows_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright (c) 2023 Murex
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
package cmd
24+
25+
import (
26+
"github.com/stretchr/testify/assert"
27+
"golang.org/x/text/encoding/charmap"
28+
"testing"
29+
)
30+
31+
func Test_windows_shell_encoding(t *testing.T) {
32+
// Windows1252 works for Western character set. We may need a more sophisticated
33+
// approach if we want to cover users with other character sets.
34+
assert.Equal(t, charmap.Windows1252, GetShellAttributes().Encoding)
35+
}
36+
37+
func Test_windows_shell_end_of_line(t *testing.T) {
38+
assert.Equal(t, "\r\n", GetShellAttributes().EOL)
39+
}

src/vcs/p4/p4_command.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
"strings"
3030
)
3131

32-
func newP4Command() *cmd.ShellCommand {
33-
return cmd.New("p4")
32+
func newP4Command(params ...string) *cmd.ShellCommand {
33+
return cmd.New("p4", params...)
3434
}
3535

3636
// IsP4CommandAvailable indicates if p4 command is available on local machine
@@ -82,3 +82,13 @@ func traceP4Command(params ...string) error {
8282
func runP4Command(params ...string) (output []byte, err error) {
8383
return newP4Command().Run(params...)
8484
}
85+
86+
// tracePipedP4Command calls p4 command, pipes it to pipedTo command, and reports its output traces
87+
func tracePipedP4Command(pipedTo *cmd.ShellCommand, params ...string) error {
88+
return newP4Command().TraceAndPipe(pipedTo, params...)
89+
}
90+
91+
// runPipedP4Command calls p4 command, pipes it to pipedTo command, and reports its output traces
92+
func runPipedP4Command(pipedTo *cmd.ShellCommand, params ...string) (output []byte, err error) {
93+
return newP4Command().RunAndPipe(pipedTo, params...)
94+
}

src/vcs/p4/p4_command_test.go

+31-8
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,78 @@ package p4
2424

2525
import (
2626
"github.com/murex/tcr/report"
27+
"github.com/murex/tcr/vcs/cmd"
2728
"github.com/stretchr/testify/assert"
2829
"os"
2930
"strings"
3031
"testing"
3132
)
3233

33-
func skipOnGitHubActions(t *testing.T) {
34+
func skipWhenInGitHubActions(t *testing.T) {
35+
t.Helper()
3436
if os.Getenv("GITHUB_ACTIONS") == "true" {
3537
t.Skip("skipped when on GitHub Actions (no p4 environment)")
3638
}
3739
}
3840

3941
func Test_is_p4_command_available(t *testing.T) {
40-
skipOnGitHubActions(t)
42+
skipWhenInGitHubActions(t)
4143
assert.True(t, IsP4CommandAvailable())
4244
}
4345

4446
func Test_get_p4_command_path(t *testing.T) {
45-
skipOnGitHubActions(t)
47+
skipWhenInGitHubActions(t)
4648
assert.NotZero(t, GetP4CommandPath())
4749
}
4850

4951
func Test_run_p4_command(t *testing.T) {
50-
skipOnGitHubActions(t)
52+
skipWhenInGitHubActions(t)
5153
output, err := runP4Command("info")
5254
assert.NoError(t, err)
5355
assert.NotZero(t, output)
5456
}
5557

5658
func Test_trace_p4_command(t *testing.T) {
57-
skipOnGitHubActions(t)
59+
skipWhenInGitHubActions(t)
5860
sniffer := report.NewSniffer()
5961
err := traceP4Command("info")
6062
sniffer.Stop()
6163
assert.NoError(t, err)
6264
assert.NotZero(t, sniffer.GetMatchCount())
6365
}
6466

67+
func Test_run_piped_p4_command(t *testing.T) {
68+
skipWhenInGitHubActions(t)
69+
output, err := runPipedP4Command(
70+
cmd.New("grep", "Client name"),
71+
"info")
72+
assert.NoError(t, err)
73+
assert.Contains(t, string(output), "Client name:")
74+
}
75+
76+
func Test_trace_piped_p4_command(t *testing.T) {
77+
skipWhenInGitHubActions(t)
78+
sniffer := report.NewSniffer()
79+
err := tracePipedP4Command(
80+
cmd.New("grep", "Client name"),
81+
"info")
82+
sniffer.Stop()
83+
assert.NoError(t, err)
84+
assert.Equal(t, 1, sniffer.GetMatchCount())
85+
assert.Contains(t, sniffer.GetAllMatches()[0].Text, "Client name:")
86+
}
87+
6588
func Test_get_p4_username(t *testing.T) {
66-
skipOnGitHubActions(t)
89+
skipWhenInGitHubActions(t)
6790
assert.NotZero(t, GetP4UserName())
6891
}
6992

7093
func Test_get_p4_config_value_with_undefined_key(t *testing.T) {
71-
skipOnGitHubActions(t)
94+
skipWhenInGitHubActions(t)
7295
assert.Equal(t, "not set", getP4ConfigValue("undefined-config-value"))
7396
}
7497

7598
func Test_get_p4_command_version(t *testing.T) {
76-
skipOnGitHubActions(t)
99+
skipWhenInGitHubActions(t)
77100
assert.True(t, strings.HasPrefix(GetP4CommandVersion(), "P4/"))
78101
}

0 commit comments

Comments
 (0)