diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/add_test.go | 32 | ||||
-rw-r--r-- | commands/env_testing.go | 48 | ||||
-rw-r--r-- | commands/rm_test.go | 17 | ||||
-rw-r--r-- | commands/user_create_test.go | 36 |
4 files changed, 133 insertions, 0 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_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")) +} |