X Tutup
Skip to content

Commit fc025e1

Browse files
committed
FIX docker-archive-public#2746 Improve shell detection in bugsnag reports
Signed-off-by: David Gageot <david@gageot.net>
1 parent ad3e6b5 commit fc025e1

File tree

7 files changed

+25
-26
lines changed

7 files changed

+25
-26
lines changed

commands/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"text/template"
1010

1111
"github.com/docker/machine/commands/mcndirs"
12-
"github.com/docker/machine/commands/shell"
1312
"github.com/docker/machine/libmachine"
1413
"github.com/docker/machine/libmachine/check"
1514
"github.com/docker/machine/libmachine/log"
15+
"github.com/docker/machine/libmachine/shell"
1616
)
1717

1818
const (

libmachine/crashreport/crash_report.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/bugsnag/bugsnag-go"
1919
"github.com/docker/machine/libmachine/log"
20+
"github.com/docker/machine/libmachine/shell"
2021
"github.com/docker/machine/version"
2122
)
2223

@@ -125,12 +126,8 @@ func addFile(path string, metaData *bugsnag.MetaData) {
125126
}
126127

127128
func detectRunningShell(metaData *bugsnag.MetaData) {
128-
shell := os.Getenv("SHELL")
129-
if shell != "" {
130-
metaData.Add("device", "shell", shell)
131-
}
132-
shell = os.Getenv("__fish_bin_dir")
133-
if shell != "" {
129+
shell, err := shell.Detect()
130+
if err == nil {
134131
metaData.Add("device", "shell", shell)
135132
}
136133
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ import (
88
)
99

1010
func TestDetectBash(t *testing.T) {
11-
originalShell := os.Getenv("SHELL")
11+
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
1212
os.Setenv("SHELL", "/bin/bash")
13-
defer os.Setenv("SHELL", originalShell)
13+
1414
shell, err := Detect()
15-
assert.Nil(t, err)
15+
1616
assert.Equal(t, "bash", shell)
17+
assert.NoError(t, err)
1718
}
1819

1920
func TestDetectFish(t *testing.T) {
20-
originalShell := os.Getenv("SHELL")
21+
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
2122
os.Setenv("SHELL", "/bin/bash")
22-
defer os.Setenv("SHELL", originalShell)
23-
originalFishdir := os.Getenv("__fish_bin_dir")
23+
24+
defer func(fishDir string) { os.Setenv("__fish_bin_dir", fishDir) }(os.Getenv("__fish_bin_dir"))
2425
os.Setenv("__fish_bin_dir", "/usr/local/Cellar/fish/2.2.0/bin")
25-
defer os.Setenv("__fish_bin_dir", originalFishdir)
26+
2627
shell, err := Detect()
27-
assert.Nil(t, err)
28+
2829
assert.Equal(t, "fish", shell)
30+
assert.NoError(t, err)
2931
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
package shell
44

55
import (
6-
"fmt"
76
"os"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
1110
)
1211

13-
func TestUnknowShell(t *testing.T) {
14-
originalShell := os.Getenv("SHELL")
12+
func TestUnknownShell(t *testing.T) {
13+
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
1514
os.Setenv("SHELL", "")
16-
defer os.Setenv("SHELL", originalShell)
15+
1716
shell, err := Detect()
18-
fmt.Println(shell)
17+
1918
assert.Equal(t, err, ErrUnknownShell)
20-
assert.Equal(t, "", shell)
19+
assert.Empty(t, shell)
2120
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
)
99

1010
func TestDetect(t *testing.T) {
11-
originalShell := os.Getenv("SHELL")
11+
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL"))
1212
os.Setenv("SHELL", "")
13-
defer os.Setenv("SHELL", originalShell)
13+
1414
shell, err := Detect()
15-
assert.Nil(t, err)
15+
1616
assert.Equal(t, "cmd", shell)
17+
assert.NoError(t, err)
1718
}
1819

1920
func TestStartedBy(t *testing.T) {
2021
shell, err := startedBy()
21-
assert.Nil(t, err)
22-
assert.NotNil(t, shell)
22+
2323
assert.Equal(t, "go.exe", shell)
24+
assert.NoError(t, err)
2425
}

0 commit comments

Comments
 (0)
X Tutup