Skip to content

Commit c05b59d

Browse files
committed
[devtools] [help] Warn user when game is run outside the module directory
When game is run outside the module directory then help command does not work for all symbols from pi module.
1 parent 7460310 commit c05b59d

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

devtools/internal/help/help.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515
"github.com/elgopher/pi/devtools/internal/lib"
1616
)
1717

18-
var NotFound = fmt.Errorf("no help found")
18+
var (
19+
NotFound = fmt.Errorf("no help found")
20+
NotAvailable = fmt.Errorf("help for pi module not available outside Go module using Pi")
21+
)
1922

2023
func PrintHelp(topic string) error {
2124
switch topic {
@@ -66,6 +69,9 @@ func goDoc(symbol string) error {
6669
if err := command.Run(); err != nil {
6770
var exitErr *exec.ExitError
6871
if isExitErr := errors.As(err, &exitErr); isExitErr && exitErr.ExitCode() == 1 {
72+
if strings.HasPrefix(symbol, "github.com/elgopher/pi") && moduleDoesNotHaveDependencyToPi() {
73+
return NotAvailable
74+
}
6975
return NotFound
7076
}
7177

@@ -75,6 +81,12 @@ func goDoc(symbol string) error {
7581
return nil
7682
}
7783

84+
func moduleDoesNotHaveDependencyToPi() bool {
85+
cmd := exec.Command("go", "list", "-m", "github.com/elgopher/pi")
86+
err := cmd.Run()
87+
return err != nil
88+
}
89+
7890
func completeSymbol(symbol string) string {
7991
packages := lib.AllPackages()
8092

devtools/internal/help/help_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package help_test
77

88
import (
9+
"os"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214

1315
"github.com/elgopher/pi/devtools/internal/help"
1416
"github.com/elgopher/pi/devtools/internal/test"
@@ -103,4 +105,25 @@ func TestPrintHelp(t *testing.T) {
103105
})
104106
}
105107
})
108+
109+
t.Run("should return error when help is not available because game was run outside the module using Pi", func(t *testing.T) {
110+
prevWorkDir, err := os.Getwd()
111+
require.NoError(t, err)
112+
113+
tmpDir, err := os.MkdirTemp("", "")
114+
require.NoError(t, err)
115+
defer func() {
116+
_ = os.RemoveAll(tmpDir)
117+
}()
118+
119+
err = os.Chdir(tmpDir)
120+
require.NoError(t, err)
121+
defer func() {
122+
_ = os.Chdir(prevWorkDir)
123+
}()
124+
// when
125+
err = help.PrintHelp("pi.Spr")
126+
// then
127+
assert.ErrorIs(t, err, help.NotAvailable)
128+
})
106129
}

devtools/scripting.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ func evaluateNextCommandFromTerminal() {
5454
case cmd := <-terminal.Commands:
5555
result, err := interpreterInstance.Eval(cmd)
5656
if err != nil {
57-
fmt.Println(err)
57+
switch err {
58+
case help.NotAvailable:
59+
fmt.Println("Sorry, help for pi module is only available when the game has been started in the directory of the Go module that uses the Pi.")
60+
default:
61+
fmt.Println(err)
62+
}
5863
terminal.CommandsProcessed <- terminal.Done
5964

6065
return

0 commit comments

Comments
 (0)