Commit 7d36bc3b authored by Igor's avatar Igor

Merge branch 'ashmckenzie/make-console-messages-consistent' into 'master'

Make console messages consistent

See merge request gitlab-org/gitlab-shell!334
parents 629e3bf9 aa82832f
......@@ -7,6 +7,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/internal/command"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/console"
"gitlab.com/gitlab-org/gitlab-shell/internal/executable"
)
......@@ -38,7 +39,7 @@ func main() {
}
if err = cmd.Execute(); err != nil {
fmt.Fprintf(readWriter.ErrOut, "%v\n", err)
console.DisplayWarningMessage(err.Error(), readWriter.ErrOut)
os.Exit(1)
}
}
......@@ -7,6 +7,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/internal/command"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/console"
"gitlab.com/gitlab-org/gitlab-shell/internal/executable"
)
......@@ -38,7 +39,7 @@ func main() {
}
if err = cmd.Execute(); err != nil {
fmt.Fprintf(readWriter.ErrOut, "%v\n", err)
console.DisplayWarningMessage(err.Error(), readWriter.ErrOut)
os.Exit(1)
}
}
......@@ -7,6 +7,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/internal/command"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/console"
"gitlab.com/gitlab-org/gitlab-shell/internal/executable"
)
......@@ -38,7 +39,7 @@ func main() {
}
if err = cmd.Execute(); err != nil {
fmt.Fprintf(readWriter.ErrOut, "%v\n", err)
console.DisplayWarningMessage(err.Error(), readWriter.ErrOut)
os.Exit(1)
}
}
......@@ -31,12 +31,12 @@ func TestFailedRequests(t *testing.T) {
{
desc: "With missing arguments",
arguments: &commandargs.Shell{},
expectedOutput: "> GitLab: Disallowed command",
expectedOutput: "Disallowed command",
},
{
desc: "With disallowed command",
arguments: &commandargs.Shell{GitlabKeyId: "1", SshArgs: []string{"git-lfs-authenticate", "group/repo", "unknown"}},
expectedOutput: "> GitLab: Disallowed command",
expectedOutput: "Disallowed command",
},
{
desc: "With disallowed user",
......
......@@ -3,12 +3,13 @@ package receivepack
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"gitlab.com/gitlab-org/gitlab-shell/internal/console"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier"
)
......@@ -32,19 +33,11 @@ func (c *Command) processCustomAction(response *accessverifier.Response) error {
return errors.New("Custom action error: Empty API endpoints")
}
c.displayInfoMessage(data.InfoMessage)
console.DisplayInfoMessages(strings.Split(data.InfoMessage, "\n"), c.ReadWriter.ErrOut)
return c.processApiEndpoints(response)
}
func (c *Command) displayInfoMessage(infoMessage string) {
messages := strings.Split(infoMessage, "\n")
for _, msg := range messages {
fmt.Fprintf(c.ReadWriter.ErrOut, "> GitLab: %v\n", msg)
}
}
func (c *Command) processApiEndpoints(response *accessverifier.Response) error {
client, err := gitlabnet.GetClient(c.Config)
......
......@@ -100,6 +100,6 @@ func TestCustomReceivePack(t *testing.T) {
// expect printing of info message, "custom" string from the first request
// and "output" string from the second request
require.Equal(t, "> GitLab: info_message\n> GitLab: one more message\n", errBuf.String())
require.Equal(t, "remote: \nremote: info_message\nremote: one more message\nremote: \n", errBuf.String())
require.Equal(t, "customoutput", outBuf.String())
}
......@@ -2,11 +2,11 @@ package accessverifier
import (
"errors"
"fmt"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/console"
"gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier"
)
......@@ -39,7 +39,5 @@ func (c *Command) Verify(action commandargs.CommandType, repo string) (*Response
}
func (c *Command) displayConsoleMessages(messages []string) {
for _, msg := range messages {
fmt.Fprintf(c.ReadWriter.ErrOut, "> GitLab: %v\n", msg)
}
console.DisplayInfoMessages(messages, c.ReadWriter.ErrOut)
}
......@@ -77,6 +77,6 @@ func TestConsoleMessages(t *testing.T) {
cmd.Args = &commandargs.Shell{GitlabKeyId: "1"}
cmd.Verify(action, repo)
require.Equal(t, "> GitLab: console\n> GitLab: message\n", errBuf.String())
require.Equal(t, "remote: \nremote: console\nremote: message\nremote: \n", errBuf.String())
require.Empty(t, outBuf.String())
}
......@@ -3,5 +3,5 @@ package disallowedcommand
import "errors"
var (
Error = errors.New("> GitLab: Disallowed command")
Error = errors.New("Disallowed command")
)
package console
import (
"fmt"
"io"
"strings"
)
func DisplayWarningMessage(message string, out io.Writer) {
DisplayWarningMessages([]string{message}, out)
}
func DisplayInfoMessage(message string, out io.Writer) {
DisplayInfoMessages([]string{message}, out)
}
func DisplayWarningMessages(messages []string, out io.Writer) {
DisplayMessages(messages, out, true)
}
func DisplayInfoMessages(messages []string, out io.Writer) {
DisplayMessages(messages, out, false)
}
func DisplayMessages(messages []string, out io.Writer, displayDivider bool) {
if noMessages(messages) {
return
}
displayBlankLineOrDivider(out, displayDivider)
for _, msg := range messages {
fmt.Fprintf(out, formatLine(msg))
}
displayBlankLineOrDivider(out, displayDivider)
}
func noMessages(messages []string) bool {
if len(messages) == 0 {
return true
}
for _, msg := range messages {
if len(strings.TrimSpace(msg)) > 0 {
return false
}
}
return true
}
func formatLine(message string) string {
return fmt.Sprintf("remote: %v\n", message)
}
func displayBlankLineOrDivider(out io.Writer, displayDivider bool) {
if displayDivider {
fmt.Fprintf(out, divider())
} else {
fmt.Fprintf(out, blankLine())
}
}
func blankLine() string {
return formatLine("")
}
func divider() string {
ruler := strings.Repeat("=", 72)
return fmt.Sprintf("%v%v%v", blankLine(), formatLine(ruler), blankLine())
}
package console
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
func TestDisplayWarningMessage(t *testing.T) {
tests := []struct {
name string
message string
wantOut string
}{
{
name: "empty",
message: "",
wantOut: "",
},
{
name: "basically empty",
message: " ",
wantOut: "",
},
{
name: "something",
message: "something",
wantOut: `remote:
remote: ========================================================================
remote:
remote: something
remote:
remote: ========================================================================
remote:
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
DisplayWarningMessage(tt.message, out)
require.Equal(t, tt.wantOut, out.String())
})
}
}
func TestDisplayWarningMessages(t *testing.T) {
tests := []struct {
name string
messages []string
wantOut string
}{
{
name: "empty",
messages: []string{""},
wantOut: "",
},
{
name: "basically empty",
messages: []string{" "},
wantOut: "",
},
{
name: "something",
messages: []string{"something", "here"},
wantOut: `remote:
remote: ========================================================================
remote:
remote: something
remote: here
remote:
remote: ========================================================================
remote:
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
DisplayWarningMessages(tt.messages, out)
require.Equal(t, tt.wantOut, out.String())
})
}
}
func TestDisplayInfoMessage(t *testing.T) {
tests := []struct {
name string
message string
wantOut string
}{
{
name: "empty",
message: "",
wantOut: "",
},
{
name: "basically empty",
message: " ",
wantOut: "",
},
{
name: "something",
message: "something",
wantOut: `remote:
remote: something
remote:
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
DisplayInfoMessage(tt.message, out)
require.Equal(t, tt.wantOut, out.String())
})
}
}
func TestDisplayInfoMessages(t *testing.T) {
tests := []struct {
name string
messages []string
wantOut string
}{
{
name: "empty",
messages: []string{""},
wantOut: "",
},
{
name: "basically empty",
messages: []string{" "},
wantOut: "",
},
{
name: "something",
messages: []string{"something", "here"},
wantOut: "remote: \nremote: something\nremote: here\nremote: \n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
DisplayInfoMessages(tt.messages, out)
require.Equal(t, tt.wantOut, out.String())
})
}
}
func Test_noMessages(t *testing.T) {
tests := []struct {
name string
messages []string
want bool
}{
{
name: "empty",
messages: []string{""},
want: true,
},
{
name: "basically empty",
messages: []string{" "},
want: true,
},
{
name: "something",
messages: []string{"something", "here"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, noMessages(tt.messages))
})
}
}
func Test_formatLine(t *testing.T) {
require.Equal(t, "remote: something\n", formatLine("something"))
}
func Test_divider(t *testing.T) {
want := `remote:
remote: ========================================================================
remote:
`
require.Equal(t, want, divider())
}
......@@ -8,6 +8,7 @@ describe 'Custom bin/gitlab-shell git-receive-pack' do
include_context 'gitlab shell'
let(:env) { {'SSH_CONNECTION' => 'fake', 'SSH_ORIGINAL_COMMAND' => 'git-receive-pack group/repo' } }
let(:divider) { "remote: ========================================================================\n" }
before(:context) do
write_config("gitlab_url" => "http+unix://#{CGI.escape(tmp_socket_path)}")
......@@ -65,21 +66,24 @@ describe 'Custom bin/gitlab-shell git-receive-pack' do
end
describe 'dialog for performing a custom action' do
let(:inaccessible_error) { "Internal API error (403)\n" }
context 'when API calls perform successfully' do
let(:remote_blank_line) { "remote: \n" }
def verify_successful_call!(cmd)
Open3.popen3(env, cmd) do |stdin, stdout, stderr|
expect(stderr.gets).to eq("> GitLab: console\n")
expect(stderr.gets).to eq("> GitLab: message\n")
expect(stderr.gets).to eq(remote_blank_line)
expect(stderr.gets).to eq("remote: console\n")
expect(stderr.gets).to eq("remote: message\n")
expect(stderr.gets).to eq(remote_blank_line)
expect(stderr.gets).to eq("> GitLab: info_message\n")
expect(stderr.gets).to eq("> GitLab: another_message\n")
expect(stdout.gets(6)).to eq("custom")
expect(stderr.gets).to eq(remote_blank_line)
expect(stderr.gets).to eq("remote: info_message\n")
expect(stderr.gets).to eq("remote: another_message\n")
expect(stderr.gets).to eq(remote_blank_line)
stdin.puts("input")
stdin.close
expect(stdout.gets(6)).to eq("custom")
expect(stdout.flush.read).to eq("input\n")
end
end
......@@ -106,7 +110,13 @@ describe 'Custom bin/gitlab-shell git-receive-pack' do
it 'custom action is not performed' do
Open3.popen2e(env, cmd) do |stdin, stdout|
expect(stdout.gets).to eq(inaccessible_error)
expect(stdout.gets).to eq("remote: \n")
expect(stdout.gets).to eq(divider)
expect(stdout.gets).to eq("remote: \n")
expect(stdout.gets).to eq("remote: Internal API error (403)\n")
expect(stdout.gets).to eq("remote: \n")
expect(stdout.gets).to eq(divider)
expect(stdout.gets).to eq("remote: \n")
end
end
end
......
......@@ -117,9 +117,10 @@ describe 'bin/gitlab-shell git-lfs-authentication' do
let(:env) { {'SSH_CONNECTION' => 'fake', 'SSH_ORIGINAL_COMMAND' => 'git-lfs-authenticate project/repo unknown' } }
it 'the command is disallowed' do
divider = "remote: \nremote: ========================================================================\nremote: \n"
_, stderr, status = Open3.capture3(env, cmd)
expect(stderr).to eq("> GitLab: Disallowed command\n")
expect(stderr).to eq("#{divider}remote: Disallowed command\n#{divider}")
expect(status).not_to be_success
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment