aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
Diffstat (limited to 'commands')
-rw-r--r--commands/add_test.go32
-rw-r--r--commands/env.go4
-rw-r--r--commands/env_testing.go48
-rw-r--r--commands/rm_test.go17
-rw-r--r--commands/user_create_test.go36
5 files changed, 136 insertions, 1 deletions
diff --git a/commands/add_test.go b/commands/add_test.go
new file mode 100644
index 00000000..63eda06e
--- /dev/null
+++ b/commands/add_test.go
@@ -0,0 +1,32 @@
+package commands
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func newTestEnvUserAndBug(t *testing.T) (*testEnv, string, string) {
+ t.Helper()
+
+ testEnv, userID := newTestEnvAndUser(t)
+ opts := addOptions{
+ title: "this is a bug title",
+ message: "this is a bug message",
+ messageFile: "",
+ nonInteractive: true,
+ }
+
+ require.NoError(t, runAdd(testEnv.env, opts))
+ require.Regexp(t, "^[0-9A-Fa-f]{7} created\n$", testEnv.out)
+ bugID := strings.Split(testEnv.out.String(), " ")[0]
+ testEnv.out.Reset()
+
+ return testEnv, userID, bugID
+}
+
+func TestAdd(t *testing.T) {
+ _, _, user := newTestEnvUserAndBug(t)
+ require.Regexp(t, "^[0-9A-Fa-f]{7}$", user)
+}
diff --git a/commands/env.go b/commands/env.go
index ac7b789a..a6bca7e4 100644
--- a/commands/env.go
+++ b/commands/env.go
@@ -14,6 +14,8 @@ import (
"github.com/MichaelMure/git-bug/util/interrupt"
)
+const gitBugNamespace = "git-bug"
+
// Env is the environment of a command
type Env struct {
repo repository.ClockedRepo
@@ -54,7 +56,7 @@ func loadRepo(env *Env) func(*cobra.Command, []string) error {
return fmt.Errorf("unable to get the current working directory: %q", err)
}
- env.repo, err = repository.OpenGoGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader})
+ env.repo, err = repository.OpenGoGitRepo(cwd, gitBugNamespace, []repository.ClockLoader{bug.ClockLoader})
if err == repository.ErrNotARepo {
return fmt.Errorf("%s must be run from within a git repo", rootCommandName)
}
diff --git a/commands/env_testing.go b/commands/env_testing.go
new file mode 100644
index 00000000..4de66a9d
--- /dev/null
+++ b/commands/env_testing.go
@@ -0,0 +1,48 @@
+package commands
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+type testEnv struct {
+ env *Env
+ cwd string
+ out *bytes.Buffer
+}
+
+func newTestEnv(t *testing.T) *testEnv {
+ t.Helper()
+
+ cwd := t.TempDir()
+
+ repo, err := repository.InitGoGitRepo(cwd, gitBugNamespace)
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ require.NoError(t, repo.Close())
+ })
+
+ buf := new(bytes.Buffer)
+
+ backend, err := cache.NewRepoCache(repo)
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ backend.Close()
+ })
+
+ return &testEnv{
+ env: &Env{
+ repo: repo,
+ backend: backend,
+ out: out{Writer: buf},
+ err: out{Writer: buf},
+ },
+ cwd: cwd,
+ out: buf,
+ }
+}
diff --git a/commands/rm_test.go b/commands/rm_test.go
new file mode 100644
index 00000000..5d4e7cca
--- /dev/null
+++ b/commands/rm_test.go
@@ -0,0 +1,17 @@
+package commands
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestRm(t *testing.T) {
+ testEnv, _, bugID := newTestEnvUserAndBug(t)
+
+ exp := "bug " + bugID + " removed\n"
+
+ require.NoError(t, runRm(testEnv.env, []string{bugID}))
+ require.Equal(t, exp, testEnv.out.String())
+ testEnv.out.Reset()
+}
diff --git a/commands/user_create_test.go b/commands/user_create_test.go
new file mode 100644
index 00000000..223e7ec3
--- /dev/null
+++ b/commands/user_create_test.go
@@ -0,0 +1,36 @@
+package commands
+
+import (
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func newTestEnvAndUser(t *testing.T) (*testEnv, string) {
+ t.Helper()
+
+ testEnv := newTestEnv(t)
+
+ opts := createUserOptions{
+ name: "John Doe",
+ email: "jdoe@example.com",
+ avatarURL: "",
+ nonInteractive: true,
+ }
+
+ require.NoError(t, runUserCreate(testEnv.env, opts))
+
+ userID := strings.TrimSpace(testEnv.out.String())
+ testEnv.out.Reset()
+
+ return testEnv, userID
+}
+
+func TestUserCreateCommand(t *testing.T) {
+ testEnv, userID := newTestEnvAndUser(t)
+
+ require.FileExists(t, filepath.Join(testEnv.cwd, ".git", "refs", "identities", userID))
+ require.FileExists(t, filepath.Join(testEnv.cwd, ".git", "git-bug", "identity-cache"))
+}