Skip to content

Commit

Permalink
Address comments by @sinadarbouy
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Oct 3, 2024
1 parent 07f1338 commit d7cf221
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
8 changes: 8 additions & 0 deletions cmd/cmd_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"

Expand Down Expand Up @@ -44,3 +45,10 @@ func mustPullPlugin() (string, error) {

return filepath.Abs(fileName) //nolint:wrapcheck
}

// runCommand runs a command in a given directory.
func runCommand(dir string, command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Dir = dir
return cmd.Run()

Check failure on line 53 in cmd/cmd_helpers_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

error returned from external package is unwrapped: sig: func (*os/exec.Cmd).Run() error (wrapcheck)
}
36 changes: 16 additions & 20 deletions cmd/plugin_scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
Expand Down Expand Up @@ -34,40 +33,38 @@ func Test_pluginScaffoldCmd(t *testing.T) {
pluginTestScaffoldInputFile := "./testdata/scaffold_input.yaml"

output, err := executeCommandC(
rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile)
rootCmd, "plugin", "scaffold", "-i", pluginTestScaffoldInputFile, "-o", t.TempDir())
require.NoError(t, err, "plugin scaffold should not return an error")
assert.Contains(t, output, "scaffold done")
assert.Contains(t, output, "created files:")
assert.Contains(t, output, "test-gatewayd-plugin/.github/issue_template.md")
assert.Contains(t, output, "test-gatewayd-plugin/.github/pull_request_template.md")
assert.Contains(t, output, "test-gatewayd-plugin/.github/workflows/commits-signed.yaml")

pluginsConfig, err := os.ReadFile(
filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugin.yaml"))
pluginName := "test-gatewayd-plugin"
pluginDir := filepath.Join(t.TempDir(), pluginName)

pluginsConfig, err := os.ReadFile(filepath.Join(pluginDir, "gatewayd_plugin.yaml"))
require.NoError(t, err, "reading plugins config file should not return an error")

var localPluginsConfig map[string]interface{}
err = yamlv3.Unmarshal(pluginsConfig, &localPluginsConfig)
require.NoError(t, err, "unmarshalling yaml file should not return error")

tidy := exec.Command("go", "mod", "tidy")
tidy.Dir = filepath.Join("plugins", "test-gatewayd-plugin")
err = tidy.Run()
assert.NoError(t, err)
err = runCommand(pluginDir, "go", "mod", "tidy")
require.NoError(t, err, "running go mod tidy should not return an error")
runCommand(pluginDir, "make", "build-dev")

Check failure on line 56 in cmd/plugin_scaffold_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

Error return value is not checked (errcheck)
require.NoError(t, err, "running make build-dev should not return an error")

build := exec.Command("make", "build-dev")
build.Dir = filepath.Join("plugins", "test-gatewayd-plugin")
err = build.Run()
assert.NoError(t, err)
pluginBinaryPath := filepath.Join(pluginDir, pluginName)

_, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin"))
require.NoError(t, err, "reading plugin binary file should not return an error")
_, err = os.Stat(pluginBinaryPath)
require.NoError(t, err, "plugin binary file should exist")

pluginsList := cast.ToSlice(localPluginsConfig["plugins"])
plugin := cast.ToStringMap(pluginsList[0])
pluginsList[0] = plugin
plugin["localPath"] = filepath.Join("cmd", "plugins", "test-gatewayd-plugin", "test-gatewayd-plugin")
sum, err := checksum.SHA256sum(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin"))
plugin["localPath"] = filepath.Join("cmd", pluginDir, pluginName)
sum, err := checksum.SHA256sum(pluginBinaryPath)
require.NoError(t, err, "marshalling yaml file should not return error")
plugin["checksum"] = sum

Expand All @@ -79,12 +76,11 @@ func Test_pluginScaffoldCmd(t *testing.T) {
require.NoError(t, err, "marshalling yaml file should not return error")

err = os.WriteFile(
filepath.Join("plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml"),
filepath.Join(pluginDir, "gatewayd_plugins.yaml"),
updatedPluginConfig, FilePermissions)
require.NoError(t, err, "writingh to yaml file should not return error")

pluginTestConfigFile := filepath.Join(
"plugins", "test-gatewayd-plugin", "gatewayd_plugins.yaml")
pluginTestConfigFile := filepath.Join(pluginDir, "gatewayd_plugins.yaml")

stopChan = make(chan struct{})

Expand Down

0 comments on commit d7cf221

Please sign in to comment.