Skip to content

Commit

Permalink
[devtools] [help] Warn user when game is run outside the module direc…
Browse files Browse the repository at this point in the history
…tory

When game is run outside the module directory then help command does not work for all symbols from pi module.
  • Loading branch information
elgopher committed Aug 15, 2023
1 parent 7460310 commit c05b59d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
14 changes: 13 additions & 1 deletion devtools/internal/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
"github.com/elgopher/pi/devtools/internal/lib"
)

var NotFound = fmt.Errorf("no help found")
var (
NotFound = fmt.Errorf("no help found")
NotAvailable = fmt.Errorf("help for pi module not available outside Go module using Pi")
)

func PrintHelp(topic string) error {
switch topic {
Expand Down Expand Up @@ -66,6 +69,9 @@ func goDoc(symbol string) error {
if err := command.Run(); err != nil {
var exitErr *exec.ExitError
if isExitErr := errors.As(err, &exitErr); isExitErr && exitErr.ExitCode() == 1 {
if strings.HasPrefix(symbol, "github.com/elgopher/pi") && moduleDoesNotHaveDependencyToPi() {
return NotAvailable
}
return NotFound
}

Expand All @@ -75,6 +81,12 @@ func goDoc(symbol string) error {
return nil
}

func moduleDoesNotHaveDependencyToPi() bool {
cmd := exec.Command("go", "list", "-m", "github.com/elgopher/pi")
err := cmd.Run()
return err != nil
}

func completeSymbol(symbol string) string {
packages := lib.AllPackages()

Expand Down
23 changes: 23 additions & 0 deletions devtools/internal/help/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package help_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elgopher/pi/devtools/internal/help"
"github.com/elgopher/pi/devtools/internal/test"
Expand Down Expand Up @@ -103,4 +105,25 @@ func TestPrintHelp(t *testing.T) {
})
}
})

t.Run("should return error when help is not available because game was run outside the module using Pi", func(t *testing.T) {
prevWorkDir, err := os.Getwd()
require.NoError(t, err)

tmpDir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tmpDir)
}()

err = os.Chdir(tmpDir)
require.NoError(t, err)
defer func() {
_ = os.Chdir(prevWorkDir)
}()
// when
err = help.PrintHelp("pi.Spr")
// then
assert.ErrorIs(t, err, help.NotAvailable)
})
}
7 changes: 6 additions & 1 deletion devtools/scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ func evaluateNextCommandFromTerminal() {
case cmd := <-terminal.Commands:
result, err := interpreterInstance.Eval(cmd)
if err != nil {
fmt.Println(err)
switch err {
case help.NotAvailable:
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.")
default:
fmt.Println(err)
}
terminal.CommandsProcessed <- terminal.Done

return
Expand Down

0 comments on commit c05b59d

Please sign in to comment.