From 20a845142722fea74753d64d9488235a85927a7f Mon Sep 17 00:00:00 2001 From: jolheiser Date: Sun, 23 Feb 2025 11:58:10 -0600 Subject: [PATCH] add tests --- go.mod | 3 +++ go.mod.sri | 2 +- internal/git/git_test.go | 45 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 internal/git/git_test.go diff --git a/go.mod b/go.mod index f45ce70..1f0c033 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.1 toolchain go1.23.3 require ( + github.com/alecthomas/assert/v2 v2.11.0 github.com/alecthomas/chroma/v2 v2.15.0 github.com/charmbracelet/ssh v0.0.0-20241211182756-4fe22b0f1b7c github.com/charmbracelet/wish v1.4.4 @@ -25,6 +26,7 @@ require ( dario.cat/mergo v1.0.1 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/ProtonMail/go-crypto v1.1.4 // indirect + github.com/alecthomas/repr v0.4.0 // indirect github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/charmbracelet/bubbletea v1.2.4 // indirect @@ -46,6 +48,7 @@ require ( github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect + github.com/hexops/gotextdiff v1.0.3 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect diff --git a/go.mod.sri b/go.mod.sri index 2e15174..c3162da 100644 --- a/go.mod.sri +++ b/go.mod.sri @@ -1 +1 @@ -sha256-+pv4dv79+/WUMiPmlK9eebS3NR6aT8q/Cw+MZ5BWoxg= \ No newline at end of file +sha256-L87PnM43gHrDcsRr3wnkB4e1Th2S0LsSwkXuebAFH44= \ No newline at end of file diff --git a/internal/git/git_test.go b/internal/git/git_test.go new file mode 100644 index 0000000..fd5ee80 --- /dev/null +++ b/internal/git/git_test.go @@ -0,0 +1,45 @@ +package git_test + +import ( + "path/filepath" + "testing" + + "github.com/alecthomas/assert/v2" + "go.jolheiser.com/ugit/internal/git" +) + +func TestEnsureRepo(t *testing.T) { + tmp := t.TempDir() + + ok, err := git.PathExists(filepath.Join(tmp, "test")) + assert.False(t, ok, "repo should not exist yet") + assert.NoError(t, err, "PathExists should not error when repo doesn't exist") + + err = git.EnsureRepo(tmp, "test") + assert.NoError(t, err, "repo should be created") + + ok, err = git.PathExists(filepath.Join(tmp, "test")) + assert.True(t, ok, "repo should exist") + assert.NoError(t, err, "EnsureRepo should not error when path exists") + + err = git.EnsureRepo(tmp, "test") + assert.NoError(t, err, "repo should already exist") +} + +func TestRepo(t *testing.T) { + tmp := t.TempDir() + err := git.EnsureRepo(tmp, "test.git") + assert.NoError(t, err, "should create repo") + + repo, err := git.NewRepo(tmp, "test") + assert.NoError(t, err, "should init new repo") + assert.True(t, repo.Meta.Private, "repo should default to private") + + repo.Meta.Private = false + err = repo.SaveMeta() + assert.NoError(t, err, "should save repo meta") + + repo, err = git.NewRepo(tmp, "test") + assert.NoError(t, err, "should not error when getting existing repo") + assert.False(t, repo.Meta.Private, "repo should be public after saving meta") +}