From 8d6b6bc1c69f71e67377152d4f7deac72be501d4 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 08:26:44 +0900 Subject: [PATCH 01/25] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Capitalize=20default?= =?UTF-8?q?Options=20to=20DefaultOptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/help.go | 2 +- core/options.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/help.go b/core/help.go index 0893485..ec1aa9e 100644 --- a/core/help.go +++ b/core/help.go @@ -19,7 +19,7 @@ func Help() { * Use interactive mode for command which requires input such as "npx sv create" for SvelteKit. git exec -i npx sv create my-app ` - optionItems, envVarItems := opts.HelpItemsAndEnvVarMappings[Options](defaultOptions, optionTypes) + optionItems, envVarItems := opts.HelpItemsAndEnvVarMappings[Options](DefaultOptions, optionTypes) options := "Options:\n" + strings.Join(optionItems, "\n") envVars := "Environment variable mapping:\n" + strings.Join(envVarItems, "\n") diff --git a/core/options.go b/core/options.go index acbb548..80f7572 100644 --- a/core/options.go +++ b/core/options.go @@ -19,7 +19,7 @@ type Options struct { Interactive bool } -var defaultOptions = &Options{ +var DefaultOptions = &Options{ Help: false, Version: false, Directory: "", @@ -79,9 +79,9 @@ var optionTypes = func() []*opts.Definition[Options] { }() func newOptions() *Options { - return opts.NewOptions(optionTypes, defaultOptions) + return opts.NewOptions(optionTypes, DefaultOptions) } func ParseOptions(args []string) (*Options, []string, error) { - return opts.Parse(defaultOptions, optionTypes, args...) + return opts.Parse(DefaultOptions, optionTypes, args...) } From 14ef3448f87e27d858971ea80a2037d7174a62e8 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 08:45:14 +0900 Subject: [PATCH 02/25] =?UTF-8?q?=E2=9C=85=20Add=20the=20simplest=20test?= =?UTF-8?q?=20scenario?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/grounds/.gitignore | 4 ++++ tests/scenes/01_basic/Makefile | 17 ++++++++++++++ tests/scenes/01_basic/happy_path_test.go | 29 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tests/grounds/.gitignore create mode 100644 tests/scenes/01_basic/Makefile create mode 100644 tests/scenes/01_basic/happy_path_test.go diff --git a/tests/grounds/.gitignore b/tests/grounds/.gitignore new file mode 100644 index 0000000..4c70690 --- /dev/null +++ b/tests/grounds/.gitignore @@ -0,0 +1,4 @@ +* +.* +*.* +!.gitignore diff --git a/tests/scenes/01_basic/Makefile b/tests/scenes/01_basic/Makefile new file mode 100644 index 0000000..a3aa6e8 --- /dev/null +++ b/tests/scenes/01_basic/Makefile @@ -0,0 +1,17 @@ +README.md: + @echo "Generating README.md" + @echo "This is a test" > README.md + +WORK_FILE=work.txt + +.PHONY: add-one +add-one: + @echo "One" >> $(WORK_FILE) + +.PHONY: add-two +add-two: + @echo "Two" >> $(WORK_FILE) + +.PHONY: add-three +add-three: + @echo "Three" >> $(WORK_FILE) diff --git a/tests/scenes/01_basic/happy_path_test.go b/tests/scenes/01_basic/happy_path_test.go new file mode 100644 index 0000000..4b727b0 --- /dev/null +++ b/tests/scenes/01_basic/happy_path_test.go @@ -0,0 +1,29 @@ +package basic + +import ( + "testing" + + "github.com/akm/git-exec/core" + "github.com/akm/git-exec/testdir" + "github.com/akm/git-exec/testexec" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestHappyPath(t *testing.T) { + defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() + testexec.Run(t, "git", "init") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "Initial commit") + + // testexec.Run(t, "make", "README.md") + err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) + require.NoError(t, err) + + commitMessage := testexec.Stdout(t, "git", "log", "-1", "--pretty=%B") + assert.Equal(t, `đŸ€– [01_basic] $ make README.md + +Generating README.md + +`, commitMessage) +} From f9dbaf3d5cb80f07601f3fa63d50b0a3e765cfb2 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 09:13:01 +0900 Subject: [PATCH 03/25] =?UTF-8?q?=E2=9C=A8=20Add=20testdir=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testdir/git.go | 35 +++++++++++++++++ testdir/setup.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 testdir/git.go create mode 100644 testdir/setup.go diff --git a/testdir/git.go b/testdir/git.go new file mode 100644 index 0000000..c6cc8f7 --- /dev/null +++ b/testdir/git.go @@ -0,0 +1,35 @@ +package testdir + +import ( + "os" + "path/filepath" + "testing" +) + +// GitRoot searches for the directory containing the .git directory starting from the current directory and moving upwards. +// If the .git directory is found, it returns the path to that directory. +// If the .git directory is not found, it fails the test. +func GitRoot(t *testing.T) string { + dir, err := os.Getwd() + if err != nil { + t.Fatalf("Failed to get current directory: %v", err) + } + + for { + if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil { + return dir + } + + parent := filepath.Dir(dir) + if parent == dir { + t.Fatalf(".git directory not found") + } + dir = parent + } +} + +// FromGitRoot returns the absolute path by joining the Git root directory with the provided relative path. +// If the .git directory is not found, it fails the test. +func FromGitRoot(t *testing.T, relpath string) string { + return filepath.Join(GitRoot(t), relpath) +} diff --git a/testdir/setup.go b/testdir/setup.go new file mode 100644 index 0000000..c8bae3d --- /dev/null +++ b/testdir/setup.go @@ -0,0 +1,98 @@ +package testdir + +import ( + "io" + "io/fs" + "os" + "path/filepath" + "testing" +) + +func Setup(t testing.TB, srcDir, destDir string) func() { + // srcDir を destDir ă«ă‚łăƒ”ăƒŒă—ăŠă€ă‚łăƒ”ăƒŒă•ă‚ŒăŸăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘă«ă‚«ăƒŹăƒłăƒˆăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘを移拕する + // æˆ»ă‚Šć€€ăŻ ă‚«ăƒŹăƒłăƒˆăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘを慃ぼディレクトăƒȘă«æˆ»ă—ă€ă‚łăƒ”ăƒŒă•ă‚ŒăŸăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘă‚’ć‰Šé™€ă™ă‚‹é–ąæ•° + + groundDir := filepath.Join(destDir, filepath.Base(srcDir)) + if err := os.MkdirAll(destDir, os.ModePerm); err != nil { + t.Fatalf("Failed to create directory: %v", err) + } + + // Copy srcDir to destDir + if err := copyDir(srcDir, groundDir); err != nil { + t.Fatalf("Failed to copy directory: %v", err) + } + + // Save the current working directory + origWd, err := os.Getwd() + if err != nil { + t.Fatalf("Failed to get current directory: %v", err) + } + + // Change to the new directory + if err := os.Chdir(groundDir); err != nil { + t.Fatalf("Failed to change directory: %v", err) + } + + // Return a function to restore the original directory and clean up + return func() { + // Change back to the original directory + if err := os.Chdir(origWd); err != nil { + t.Fatalf("Failed to change back to original directory: %v", err) + } + + // Remove the copied directory + if err := os.RemoveAll(groundDir); err != nil { + t.Fatalf("Failed to remove directory %s: %v", groundDir, err) + } + } +} + +// Helper function to copy a directory +func copyDir(src string, dest string) error { + entries, err := os.ReadDir(src) + if err != nil { + return err + } + + err = os.MkdirAll(dest, os.ModePerm) + if err != nil { + return err + } + + for _, entry := range entries { + if err := copyEntry(src, dest, entry); err != nil { + return err + } + } + + return nil +} + +func copyEntry(src string, dest string, entry fs.DirEntry) error { + srcPath := filepath.Join(src, entry.Name()) + destPath := filepath.Join(dest, entry.Name()) + + if entry.IsDir() { + if err := copyDir(srcPath, destPath); err != nil { + return err + } + return nil + } + + reader, err := os.Open(srcPath) + if err != nil { + return err + } + defer reader.Close() + + writer, err := os.Create(destPath) + if err != nil { + return err + } + defer writer.Close() + + if _, err := io.Copy(writer, reader); err != nil { + return err + } + return nil +} From af68e0c5c3def730ea3c1d0b16512f22d0ec566e Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 09:16:39 +0900 Subject: [PATCH 04/25] =?UTF-8?q?=E2=9C=A8=20Add=20testexec=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testexec/cmd.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 testexec/cmd.go diff --git a/testexec/cmd.go b/testexec/cmd.go new file mode 100644 index 0000000..75b0f5b --- /dev/null +++ b/testexec/cmd.go @@ -0,0 +1,29 @@ +package testexec + +import ( + "os" + "os/exec" + "testing" +) + +func Run(t testing.TB, command string, args ...string) { + t.Helper() + + cmd := exec.Command(command, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + t.Fatalf("failed to run %s %v: %s", command, args, err) + } +} + +func Stdout(t testing.TB, command string, args ...string) string { + t.Helper() + + cmd := exec.Command(command, args...) + out, err := cmd.Output() + if err != nil { + t.Fatalf("failed to run %s %v: %s", command, args, err) + } + return string(out) +} From 8520e3acc462c9e7671041ad0bb774bfeecbb204 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 09:19:26 +0900 Subject: [PATCH 05/25] =?UTF-8?q?=F0=9F=94=A8=20Make=20test=20run=20also?= =?UTF-8?q?=20for=20sub=20directories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ca4d825..faec5af 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ build: .PHONY: test test: - go test -v . + go test -v ./... GOLANGCI_LINT_VERSION=v1.61.0 GOLANGCI_LINT = $(shell go env GOPATH)/bin/golangci-lint From 05ab823f4786e946ef8b6d88c210cd82f23a9475 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 09:28:14 +0900 Subject: [PATCH 06/25] =?UTF-8?q?=E2=9C=A8=20Use=20filepath.Abs=20to=20get?= =?UTF-8?q?=20srcDir=20name=20even=20if=20it=20is=20just=20a=20dot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testdir/setup.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testdir/setup.go b/testdir/setup.go index c8bae3d..63a2fcb 100644 --- a/testdir/setup.go +++ b/testdir/setup.go @@ -12,7 +12,12 @@ func Setup(t testing.TB, srcDir, destDir string) func() { // srcDir を destDir ă«ă‚łăƒ”ăƒŒă—ăŠă€ă‚łăƒ”ăƒŒă•ă‚ŒăŸăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘă«ă‚«ăƒŹăƒłăƒˆăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘを移拕する // æˆ»ă‚Šć€€ăŻ ă‚«ăƒŹăƒłăƒˆăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘを慃ぼディレクトăƒȘă«æˆ»ă—ă€ă‚łăƒ”ăƒŒă•ă‚ŒăŸăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘă‚’ć‰Šé™€ă™ă‚‹é–ąæ•° - groundDir := filepath.Join(destDir, filepath.Base(srcDir)) + absSrcDir, err := filepath.Abs(srcDir) + if err != nil { + t.Fatalf("Failed to get absolute path: %v", err) + } + + groundDir := filepath.Join(destDir, filepath.Base(absSrcDir)) // srcDir に . ăŒæŒ‡ćźšă•ă‚ŒăŸć Žćˆă§ă‚‚ăăźăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘćă‚’ć–ćŸ—ă™ă‚‹ if err := os.MkdirAll(destDir, os.ModePerm); err != nil { t.Fatalf("Failed to create directory: %v", err) } From 0e9798533721bcb1766a512353795f8aa2018015 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 09:51:11 +0900 Subject: [PATCH 07/25] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/01_basic/guard_test.go | 61 +++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/scenes/01_basic/guard_test.go diff --git a/tests/scenes/01_basic/guard_test.go b/tests/scenes/01_basic/guard_test.go new file mode 100644 index 0000000..a9e7968 --- /dev/null +++ b/tests/scenes/01_basic/guard_test.go @@ -0,0 +1,61 @@ +package basic + +import ( + "strings" + "testing" + + "github.com/akm/git-exec/core" + "github.com/akm/git-exec/testdir" + "github.com/akm/git-exec/testexec" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGuardUntrackedFiles(t *testing.T) { + defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() + testexec.Run(t, "git", "init") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "Initial commit") + lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + + testexec.Run(t, "make", "add-one") // Let it not be committed + + err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) + require.Error(t, err) + assert.Equal(t, `Quit processing because There are untracked files + +Untracked files: +work.txt`, err.Error()) + + // No commit should be made + currCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + assert.Equal(t, lastCommitHash, currCommitHash) +} + +func TestGuardUncommittedChanes(t *testing.T) { + defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() + testexec.Run(t, "git", "init") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "Initial commit") + + // commit add-one + testexec.Run(t, "make", "add-one") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "add one") + + lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + + testexec.Run(t, "make", "add-two") // Let it not be committed + + err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) + require.Error(t, err) + assert.Equal(t, `Quit processing because There are uncommitted changes + +Uncommitted changes: +`+strings.TrimSpace(testexec.Stdout(t, "git", "diff")), + err.Error()) + + // No commit should be made + currCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + assert.Equal(t, lastCommitHash, currCommitHash) +} From 98e880370001c7ee4bc0e999a41ff8be5e67a88d Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 09:56:10 +0900 Subject: [PATCH 08/25] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20no=20chan?= =?UTF-8?q?ges?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/01_basic/no_change_test.go | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/scenes/01_basic/no_change_test.go diff --git a/tests/scenes/01_basic/no_change_test.go b/tests/scenes/01_basic/no_change_test.go new file mode 100644 index 0000000..84063cb --- /dev/null +++ b/tests/scenes/01_basic/no_change_test.go @@ -0,0 +1,33 @@ +package basic + +import ( + "testing" + + "github.com/akm/git-exec/core" + "github.com/akm/git-exec/testdir" + "github.com/akm/git-exec/testexec" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNoChane(t *testing.T) { + defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() + testexec.Run(t, "git", "init") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "Initial commit") + + testexec.Run(t, "make", "README.md") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "$ make README.md") + + lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + + // testexec.Run(t, "make", "README.md") + err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) + require.Error(t, err) + assert.Equal(t, "No changes to commit and No untracked files", err.Error()) + + // No commit should be made + currCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + assert.Equal(t, lastCommitHash, currCommitHash) +} From 2efdede8b24954c45b28498760cbd2d428546b24 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:01:09 +0900 Subject: [PATCH 09/25] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20testground?= =?UTF-8?q?=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/01_basic/guard_test.go | 12 +++--------- tests/scenes/01_basic/happy_path_test.go | 7 ++----- tests/scenes/01_basic/no_change_test.go | 7 ++----- tests/testground/setup.go | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 tests/testground/setup.go diff --git a/tests/scenes/01_basic/guard_test.go b/tests/scenes/01_basic/guard_test.go index a9e7968..5b95c57 100644 --- a/tests/scenes/01_basic/guard_test.go +++ b/tests/scenes/01_basic/guard_test.go @@ -5,17 +5,14 @@ import ( "testing" "github.com/akm/git-exec/core" - "github.com/akm/git-exec/testdir" "github.com/akm/git-exec/testexec" + "github.com/akm/git-exec/tests/testground" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestGuardUntrackedFiles(t *testing.T) { - defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() - testexec.Run(t, "git", "init") - testexec.Run(t, "git", "add", ".") - testexec.Run(t, "git", "commit", "-m", "Initial commit") + defer testground.Setup(t)() lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") testexec.Run(t, "make", "add-one") // Let it not be committed @@ -33,10 +30,7 @@ work.txt`, err.Error()) } func TestGuardUncommittedChanes(t *testing.T) { - defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() - testexec.Run(t, "git", "init") - testexec.Run(t, "git", "add", ".") - testexec.Run(t, "git", "commit", "-m", "Initial commit") + defer testground.Setup(t)() // commit add-one testexec.Run(t, "make", "add-one") diff --git a/tests/scenes/01_basic/happy_path_test.go b/tests/scenes/01_basic/happy_path_test.go index 4b727b0..5fad39c 100644 --- a/tests/scenes/01_basic/happy_path_test.go +++ b/tests/scenes/01_basic/happy_path_test.go @@ -4,17 +4,14 @@ import ( "testing" "github.com/akm/git-exec/core" - "github.com/akm/git-exec/testdir" "github.com/akm/git-exec/testexec" + "github.com/akm/git-exec/tests/testground" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestHappyPath(t *testing.T) { - defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() - testexec.Run(t, "git", "init") - testexec.Run(t, "git", "add", ".") - testexec.Run(t, "git", "commit", "-m", "Initial commit") + defer testground.Setup(t)() // testexec.Run(t, "make", "README.md") err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) diff --git a/tests/scenes/01_basic/no_change_test.go b/tests/scenes/01_basic/no_change_test.go index 84063cb..804112d 100644 --- a/tests/scenes/01_basic/no_change_test.go +++ b/tests/scenes/01_basic/no_change_test.go @@ -4,17 +4,14 @@ import ( "testing" "github.com/akm/git-exec/core" - "github.com/akm/git-exec/testdir" "github.com/akm/git-exec/testexec" + "github.com/akm/git-exec/tests/testground" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestNoChane(t *testing.T) { - defer testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds"))() - testexec.Run(t, "git", "init") - testexec.Run(t, "git", "add", ".") - testexec.Run(t, "git", "commit", "-m", "Initial commit") + defer testground.Setup(t)() testexec.Run(t, "make", "README.md") testexec.Run(t, "git", "add", ".") diff --git a/tests/testground/setup.go b/tests/testground/setup.go new file mode 100644 index 0000000..f0f0067 --- /dev/null +++ b/tests/testground/setup.go @@ -0,0 +1,19 @@ +package testground + +import ( + "testing" + + "github.com/akm/git-exec/testdir" + "github.com/akm/git-exec/testexec" +) + +func Setup(t *testing.T) func() { + t.Helper() + + r := testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds")) + testexec.Run(t, "git", "init") + testexec.Run(t, "git", "add", ".") + testexec.Run(t, "git", "commit", "-m", "Initial commit") + + return r +} From ac5504e1f40e468badb62d1f0acddd2d348e12af Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:12:54 +0900 Subject: [PATCH 10/25] =?UTF-8?q?=E2=9C=A8=20Add=20logging=20around=20test?= =?UTF-8?q?dir.Setup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testdir/setup.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/testdir/setup.go b/testdir/setup.go index 63a2fcb..1f1dba4 100644 --- a/testdir/setup.go +++ b/testdir/setup.go @@ -11,21 +11,23 @@ import ( func Setup(t testing.TB, srcDir, destDir string) func() { // srcDir を destDir ă«ă‚łăƒ”ăƒŒă—ăŠă€ă‚łăƒ”ăƒŒă•ă‚ŒăŸăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘă«ă‚«ăƒŹăƒłăƒˆăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘを移拕する // æˆ»ă‚Šć€€ăŻ ă‚«ăƒŹăƒłăƒˆăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘを慃ぼディレクトăƒȘă«æˆ»ă—ă€ă‚łăƒ”ăƒŒă•ă‚ŒăŸăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘă‚’ć‰Šé™€ă™ă‚‹é–ąæ•° + t.Logf("srcDir: %s\n", srcDir) + t.Logf("destDir: %s\n", destDir) absSrcDir, err := filepath.Abs(srcDir) if err != nil { t.Fatalf("Failed to get absolute path: %v", err) } + t.Logf("absSrcDir: %s\n", absSrcDir) groundDir := filepath.Join(destDir, filepath.Base(absSrcDir)) // srcDir に . ăŒæŒ‡ćźšă•ă‚ŒăŸć Žćˆă§ă‚‚ăăźăƒ‡ă‚ŁăƒŹă‚ŻăƒˆăƒȘćă‚’ć–ćŸ—ă™ă‚‹ + t.Logf("groundDir: %s\n", groundDir) if err := os.MkdirAll(destDir, os.ModePerm); err != nil { t.Fatalf("Failed to create directory: %v", err) } // Copy srcDir to destDir - if err := copyDir(srcDir, groundDir); err != nil { - t.Fatalf("Failed to copy directory: %v", err) - } + copyDir(t, srcDir, groundDir) // Save the current working directory origWd, err := os.Getwd() @@ -53,51 +55,48 @@ func Setup(t testing.TB, srcDir, destDir string) func() { } // Helper function to copy a directory -func copyDir(src string, dest string) error { +func copyDir(t testing.TB, src string, dest string) { + t.Logf("copyDir: %s -> %s\n", src, dest) + entries, err := os.ReadDir(src) if err != nil { - return err + t.Fatalf("Failed to read directory: %v", err) } err = os.MkdirAll(dest, os.ModePerm) if err != nil { - return err + t.Fatalf("Failed to create directory: %v", err) } for _, entry := range entries { - if err := copyEntry(src, dest, entry); err != nil { - return err - } + copyEntry(t, src, dest, entry) } - - return nil } -func copyEntry(src string, dest string, entry fs.DirEntry) error { +func copyEntry(t testing.TB, src string, dest string, entry fs.DirEntry) { srcPath := filepath.Join(src, entry.Name()) destPath := filepath.Join(dest, entry.Name()) + t.Logf("copyEntry: %s -> %s\n", srcPath, destPath) + if entry.IsDir() { - if err := copyDir(srcPath, destPath); err != nil { - return err - } - return nil + copyDir(t, srcPath, destPath) + return } reader, err := os.Open(srcPath) if err != nil { - return err + t.Fatalf("Failed to open source file: %v", err) } defer reader.Close() writer, err := os.Create(destPath) if err != nil { - return err + t.Fatalf("Failed to create destination file: %v", err) } defer writer.Close() if _, err := io.Copy(writer, reader); err != nil { - return err + t.Fatalf("Failed to copy file: %v", err) } - return nil } From df1ea72559870c8f278ef415fb8bafe3a7672b04 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:19:38 +0900 Subject: [PATCH 11/25] =?UTF-8?q?=E2=9C=A8=20Add=20testdir.FromGoModRoot?= =?UTF-8?q?=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testdir/git.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/testdir/git.go b/testdir/git.go index c6cc8f7..017b097 100644 --- a/testdir/git.go +++ b/testdir/git.go @@ -33,3 +33,26 @@ func GitRoot(t *testing.T) string { func FromGitRoot(t *testing.T, relpath string) string { return filepath.Join(GitRoot(t), relpath) } + +func GoModRoot(t *testing.T) string { + dir, err := os.Getwd() + if err != nil { + t.Fatalf("Failed to get current directory: %v", err) + } + + for { + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { + return dir + } + + parent := filepath.Dir(dir) + if parent == dir { + t.Fatalf("go.mod file not found") + } + dir = parent + } +} + +func FromGoModRoot(t *testing.T, relpath string) string { + return filepath.Join(GoModRoot(t), relpath) +} From 437137b799ed902475f8e0b69a51374d8403c2ab Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:20:20 +0900 Subject: [PATCH 12/25] =?UTF-8?q?=E2=9C=A8=20Use=20testdir.FromGoModRoot?= =?UTF-8?q?=20in=20testground.Setup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testground/setup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testground/setup.go b/tests/testground/setup.go index f0f0067..89c92df 100644 --- a/tests/testground/setup.go +++ b/tests/testground/setup.go @@ -10,7 +10,7 @@ import ( func Setup(t *testing.T) func() { t.Helper() - r := testdir.Setup(t, ".", testdir.FromGitRoot(t, "tests/grounds")) + r := testdir.Setup(t, ".", testdir.FromGoModRoot(t, "tests/grounds")) testexec.Run(t, "git", "init") testexec.Run(t, "git", "add", ".") testexec.Run(t, "git", "commit", "-m", "Initial commit") From 1f88cf2b87a9508cd07e74292333d188ab7f8ee8 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:37:59 +0900 Subject: [PATCH 13/25] =?UTF-8?q?=E2=9C=A8=20Skip=20.git=20directory=20in?= =?UTF-8?q?=20copyEntry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testdir/setup.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/testdir/setup.go b/testdir/setup.go index 1f1dba4..efd9a77 100644 --- a/testdir/setup.go +++ b/testdir/setup.go @@ -74,16 +74,21 @@ func copyDir(t testing.TB, src string, dest string) { } func copyEntry(t testing.TB, src string, dest string, entry fs.DirEntry) { + if entry.Name() == ".git" { + t.Logf("skip .git directory\n") + return + } + srcPath := filepath.Join(src, entry.Name()) destPath := filepath.Join(dest, entry.Name()) - t.Logf("copyEntry: %s -> %s\n", srcPath, destPath) - if entry.IsDir() { copyDir(t, srcPath, destPath) return } + t.Logf("copyEntry: %s -> %s\n", srcPath, destPath) + reader, err := os.Open(srcPath) if err != nil { t.Fatalf("Failed to open source file: %v", err) From 9e5f213af8b49f0d7a9bab741ee840d8e94aed19 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:43:15 +0900 Subject: [PATCH 14/25] =?UTF-8?q?=E2=9C=A8=20Add=20git=20configuratoin=20o?= =?UTF-8?q?n=20GitHub=20Actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testground/setup.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testground/setup.go b/tests/testground/setup.go index 89c92df..f1dc2e0 100644 --- a/tests/testground/setup.go +++ b/tests/testground/setup.go @@ -1,6 +1,7 @@ package testground import ( + "os" "testing" "github.com/akm/git-exec/testdir" @@ -10,6 +11,13 @@ import ( func Setup(t *testing.T) func() { t.Helper() + if os.Getenv("GITHUB_ACTIONS") == "true" { + // git config --global user.email "foo@example.com" + // git config --global user.name "Foo Bar" + testexec.Run(t, "git", "config", "--global", "user.email", "foo@example.com") + testexec.Run(t, "git", "config", "--global", "user.name", "Foo Bar") + } + r := testdir.Setup(t, ".", testdir.FromGoModRoot(t, "tests/grounds")) testexec.Run(t, "git", "init") testexec.Run(t, "git", "add", ".") From 3067eeb6051581a54671e5516f80de47f1f0ba60 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:45:46 +0900 Subject: [PATCH 15/25] =?UTF-8?q?=E2=9C=A8=20git=20config=20--global=20ini?= =?UTF-8?q?t.defaultBranch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testground/setup.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testground/setup.go b/tests/testground/setup.go index f1dc2e0..e9012d3 100644 --- a/tests/testground/setup.go +++ b/tests/testground/setup.go @@ -14,8 +14,10 @@ func Setup(t *testing.T) func() { if os.Getenv("GITHUB_ACTIONS") == "true" { // git config --global user.email "foo@example.com" // git config --global user.name "Foo Bar" + // git config --global init.defaultBranch main testexec.Run(t, "git", "config", "--global", "user.email", "foo@example.com") testexec.Run(t, "git", "config", "--global", "user.name", "Foo Bar") + testexec.Run(t, "git", "config", "--global", "init.defaultBranch", "main") } r := testdir.Setup(t, ".", testdir.FromGoModRoot(t, "tests/grounds")) From a3004923f966aa683d31b5ccf6300c4c12834d6a Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 10:54:09 +0900 Subject: [PATCH 16/25] =?UTF-8?q?=E2=9C=85=20Suppress=20make's=20output=20?= =?UTF-8?q?in=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testground/setup.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/testground/setup.go b/tests/testground/setup.go index e9012d3..6bcbffe 100644 --- a/tests/testground/setup.go +++ b/tests/testground/setup.go @@ -20,6 +20,9 @@ func Setup(t *testing.T) func() { testexec.Run(t, "git", "config", "--global", "init.defaultBranch", "main") } + // Suppress make's output + os.Setenv("MAKEFLAGS", "--no-print-directory") + r := testdir.Setup(t, ".", testdir.FromGoModRoot(t, "tests/grounds")) testexec.Run(t, "git", "init") testexec.Run(t, "git", "add", ".") From 868b24edf86d7f2cad0ba24438e2673029ca468e Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 15:32:49 +0900 Subject: [PATCH 17/25] =?UTF-8?q?=E2=9C=85=E2=99=BB=EF=B8=8F=20Define=20al?= =?UTF-8?q?iases=20for=20testexec=20to=20simplify=20test=20description?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/01_basic/aliases.go | 10 ++++++++++ tests/scenes/01_basic/guard_test.go | 21 ++++++++++----------- tests/scenes/01_basic/happy_path_test.go | 5 ++--- tests/scenes/01_basic/no_change_test.go | 13 ++++++------- 4 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 tests/scenes/01_basic/aliases.go diff --git a/tests/scenes/01_basic/aliases.go b/tests/scenes/01_basic/aliases.go new file mode 100644 index 0000000..2eb12dc --- /dev/null +++ b/tests/scenes/01_basic/aliases.go @@ -0,0 +1,10 @@ +package basic + +import ( + "github.com/akm/git-exec/testexec" +) + +var ( + run = testexec.Run + stdout = testexec.Stdout +) diff --git a/tests/scenes/01_basic/guard_test.go b/tests/scenes/01_basic/guard_test.go index 5b95c57..6738db6 100644 --- a/tests/scenes/01_basic/guard_test.go +++ b/tests/scenes/01_basic/guard_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/akm/git-exec/core" - "github.com/akm/git-exec/testexec" "github.com/akm/git-exec/tests/testground" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -13,9 +12,9 @@ import ( func TestGuardUntrackedFiles(t *testing.T) { defer testground.Setup(t)() - lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + lastCommitHash := stdout(t, "git", "rev-parse", "HEAD") - testexec.Run(t, "make", "add-one") // Let it not be committed + run(t, "make", "add-one") // Let it not be committed err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) require.Error(t, err) @@ -25,7 +24,7 @@ Untracked files: work.txt`, err.Error()) // No commit should be made - currCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + currCommitHash := stdout(t, "git", "rev-parse", "HEAD") assert.Equal(t, lastCommitHash, currCommitHash) } @@ -33,23 +32,23 @@ func TestGuardUncommittedChanes(t *testing.T) { defer testground.Setup(t)() // commit add-one - testexec.Run(t, "make", "add-one") - testexec.Run(t, "git", "add", ".") - testexec.Run(t, "git", "commit", "-m", "add one") + run(t, "make", "add-one") + run(t, "git", "add", ".") + run(t, "git", "commit", "-m", "add one") - lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + lastCommitHash := stdout(t, "git", "rev-parse", "HEAD") - testexec.Run(t, "make", "add-two") // Let it not be committed + run(t, "make", "add-two") // Let it not be committed err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) require.Error(t, err) assert.Equal(t, `Quit processing because There are uncommitted changes Uncommitted changes: -`+strings.TrimSpace(testexec.Stdout(t, "git", "diff")), +`+strings.TrimSpace(stdout(t, "git", "diff")), err.Error()) // No commit should be made - currCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + currCommitHash := stdout(t, "git", "rev-parse", "HEAD") assert.Equal(t, lastCommitHash, currCommitHash) } diff --git a/tests/scenes/01_basic/happy_path_test.go b/tests/scenes/01_basic/happy_path_test.go index 5fad39c..2c7d04c 100644 --- a/tests/scenes/01_basic/happy_path_test.go +++ b/tests/scenes/01_basic/happy_path_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/akm/git-exec/core" - "github.com/akm/git-exec/testexec" "github.com/akm/git-exec/tests/testground" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -13,11 +12,11 @@ import ( func TestHappyPath(t *testing.T) { defer testground.Setup(t)() - // testexec.Run(t, "make", "README.md") + // run(t, "make", "README.md") err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) require.NoError(t, err) - commitMessage := testexec.Stdout(t, "git", "log", "-1", "--pretty=%B") + commitMessage := stdout(t, "git", "log", "-1", "--pretty=%B") assert.Equal(t, `đŸ€– [01_basic] $ make README.md Generating README.md diff --git a/tests/scenes/01_basic/no_change_test.go b/tests/scenes/01_basic/no_change_test.go index 804112d..539f0e7 100644 --- a/tests/scenes/01_basic/no_change_test.go +++ b/tests/scenes/01_basic/no_change_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/akm/git-exec/core" - "github.com/akm/git-exec/testexec" "github.com/akm/git-exec/tests/testground" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -13,18 +12,18 @@ import ( func TestNoChane(t *testing.T) { defer testground.Setup(t)() - testexec.Run(t, "make", "README.md") - testexec.Run(t, "git", "add", ".") - testexec.Run(t, "git", "commit", "-m", "$ make README.md") + run(t, "make", "README.md") + run(t, "git", "add", ".") + run(t, "git", "commit", "-m", "$ make README.md") - lastCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + lastCommitHash := stdout(t, "git", "rev-parse", "HEAD") - // testexec.Run(t, "make", "README.md") + // run(t, "make", "README.md") err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) require.Error(t, err) assert.Equal(t, "No changes to commit and No untracked files", err.Error()) // No commit should be made - currCommitHash := testexec.Stdout(t, "git", "rev-parse", "HEAD") + currCommitHash := stdout(t, "git", "rev-parse", "HEAD") assert.Equal(t, lastCommitHash, currCommitHash) } From 8efb46e6456f3891906c3d2da4ad413e0e631972 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 16:24:58 +0900 Subject: [PATCH 18/25] =?UTF-8?q?=E2=9C=85=E2=99=BB=EF=B8=8F=20Extract=20A?= =?UTF-8?q?ssertStringNotChanged=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/01_basic/guard_test.go | 13 ++----------- tests/scenes/01_basic/no_change_test.go | 9 ++------- tests/testground/assertions.go | 16 ++++++++++++++++ tests/testground/git.go | 12 ++++++++++++ 4 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 tests/testground/assertions.go create mode 100644 tests/testground/git.go diff --git a/tests/scenes/01_basic/guard_test.go b/tests/scenes/01_basic/guard_test.go index 6738db6..233dd50 100644 --- a/tests/scenes/01_basic/guard_test.go +++ b/tests/scenes/01_basic/guard_test.go @@ -12,7 +12,7 @@ import ( func TestGuardUntrackedFiles(t *testing.T) { defer testground.Setup(t)() - lastCommitHash := stdout(t, "git", "rev-parse", "HEAD") + defer testground.AssertStringNotChanged(t, testground.GitLastCommitHash)() run(t, "make", "add-one") // Let it not be committed @@ -22,10 +22,6 @@ func TestGuardUntrackedFiles(t *testing.T) { Untracked files: work.txt`, err.Error()) - - // No commit should be made - currCommitHash := stdout(t, "git", "rev-parse", "HEAD") - assert.Equal(t, lastCommitHash, currCommitHash) } func TestGuardUncommittedChanes(t *testing.T) { @@ -36,8 +32,7 @@ func TestGuardUncommittedChanes(t *testing.T) { run(t, "git", "add", ".") run(t, "git", "commit", "-m", "add one") - lastCommitHash := stdout(t, "git", "rev-parse", "HEAD") - + defer testground.AssertStringNotChanged(t, testground.GitLastCommitHash)() run(t, "make", "add-two") // Let it not be committed err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) @@ -47,8 +42,4 @@ func TestGuardUncommittedChanes(t *testing.T) { Uncommitted changes: `+strings.TrimSpace(stdout(t, "git", "diff")), err.Error()) - - // No commit should be made - currCommitHash := stdout(t, "git", "rev-parse", "HEAD") - assert.Equal(t, lastCommitHash, currCommitHash) } diff --git a/tests/scenes/01_basic/no_change_test.go b/tests/scenes/01_basic/no_change_test.go index 539f0e7..fe28045 100644 --- a/tests/scenes/01_basic/no_change_test.go +++ b/tests/scenes/01_basic/no_change_test.go @@ -9,21 +9,16 @@ import ( "github.com/stretchr/testify/require" ) -func TestNoChane(t *testing.T) { +func TestNoChange(t *testing.T) { defer testground.Setup(t)() run(t, "make", "README.md") run(t, "git", "add", ".") run(t, "git", "commit", "-m", "$ make README.md") - lastCommitHash := stdout(t, "git", "rev-parse", "HEAD") + defer testground.AssertStringNotChanged(t, testground.GitLastCommitHash)() - // run(t, "make", "README.md") err := core.Run(core.DefaultOptions, []string{"make", "README.md"}) require.Error(t, err) assert.Equal(t, "No changes to commit and No untracked files", err.Error()) - - // No commit should be made - currCommitHash := stdout(t, "git", "rev-parse", "HEAD") - assert.Equal(t, lastCommitHash, currCommitHash) } diff --git a/tests/testground/assertions.go b/tests/testground/assertions.go new file mode 100644 index 0000000..92ece7c --- /dev/null +++ b/tests/testground/assertions.go @@ -0,0 +1,16 @@ +package testground + +import "testing" + +func AssertStringNotChanged(t *testing.T, getter func(t *testing.T) string) func() { + t.Helper() + before := getter(t) + + return func() { + t.Helper() + after := getter(t) + if before != after { + t.Fatalf("expected %q, but got %q", before, after) + } + } +} diff --git a/tests/testground/git.go b/tests/testground/git.go new file mode 100644 index 0000000..9e35ea4 --- /dev/null +++ b/tests/testground/git.go @@ -0,0 +1,12 @@ +package testground + +import ( + "testing" + + "github.com/akm/git-exec/testexec" +) + +func GitLastCommitHash(t *testing.T) string { + t.Helper() + return testexec.Stdout(t, "git", "rev-parse", "HEAD") +} From 94fcff3c519507f3a13a1208aa68c3d5665282b3 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 16:34:03 +0900 Subject: [PATCH 19/25] =?UTF-8?q?=E2=9C=85=20Add=20test=20about=20commit?= =?UTF-8?q?=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/01_basic/commit_message_test.go | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/scenes/01_basic/commit_message_test.go diff --git a/tests/scenes/01_basic/commit_message_test.go b/tests/scenes/01_basic/commit_message_test.go new file mode 100644 index 0000000..74e935f --- /dev/null +++ b/tests/scenes/01_basic/commit_message_test.go @@ -0,0 +1,28 @@ +package basic + +import ( + "testing" + + "github.com/akm/git-exec/core" + "github.com/akm/git-exec/tests/testground" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCommitMessage(t *testing.T) { + defer testground.Setup(t)() + + opts := *core.DefaultOptions + opts.Emoji = "đŸ­ïž" + opts.Prompt = "%" + opts.Template = `{{.Emoji}} {{.Prompt}} {{.Command}} [at {{.Location}}]` + err := core.Run(&opts, []string{"make", "README.md"}) + require.NoError(t, err) + + commitMessage := stdout(t, "git", "log", "-1", "--pretty=%B") + assert.Equal(t, `đŸ­ïž % make README.md [at 01_basic] + +Generating README.md + +`, commitMessage) +} From 7d391081294a09bf085b7cd472267658bc7fa357 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 16:56:51 +0900 Subject: [PATCH 20/25] =?UTF-8?q?=E2=9C=85=20Add=20testground.GitDiff=20fu?= =?UTF-8?q?nction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testground/git.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testground/git.go b/tests/testground/git.go index 9e35ea4..129a25e 100644 --- a/tests/testground/git.go +++ b/tests/testground/git.go @@ -10,3 +10,8 @@ func GitLastCommitHash(t *testing.T) string { t.Helper() return testexec.Stdout(t, "git", "rev-parse", "HEAD") } + +func GitDiff(t *testing.T) string { + t.Helper() + return testexec.Stdout(t, "git", "diff") +} From 7840fa70c74e45cb273d2c60727770b4e115902a Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 16:57:10 +0900 Subject: [PATCH 21/25] =?UTF-8?q?=E2=9C=85=20Add=20directory=5Foption=5Fte?= =?UTF-8?q?st.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scenes/02_subdir/Makefile | 17 ++++++++++++ tests/scenes/02_subdir/aliases.go | 10 +++++++ .../scenes/02_subdir/directory_option_test.go | 26 +++++++++++++++++++ tests/scenes/02_subdir/sub1/Makefile | 13 ++++++++++ 4 files changed, 66 insertions(+) create mode 100644 tests/scenes/02_subdir/Makefile create mode 100644 tests/scenes/02_subdir/aliases.go create mode 100644 tests/scenes/02_subdir/directory_option_test.go create mode 100644 tests/scenes/02_subdir/sub1/Makefile diff --git a/tests/scenes/02_subdir/Makefile b/tests/scenes/02_subdir/Makefile new file mode 100644 index 0000000..a3aa6e8 --- /dev/null +++ b/tests/scenes/02_subdir/Makefile @@ -0,0 +1,17 @@ +README.md: + @echo "Generating README.md" + @echo "This is a test" > README.md + +WORK_FILE=work.txt + +.PHONY: add-one +add-one: + @echo "One" >> $(WORK_FILE) + +.PHONY: add-two +add-two: + @echo "Two" >> $(WORK_FILE) + +.PHONY: add-three +add-three: + @echo "Three" >> $(WORK_FILE) diff --git a/tests/scenes/02_subdir/aliases.go b/tests/scenes/02_subdir/aliases.go new file mode 100644 index 0000000..99a71b8 --- /dev/null +++ b/tests/scenes/02_subdir/aliases.go @@ -0,0 +1,10 @@ +package basic + +import ( + "github.com/akm/git-exec/testexec" +) + +var ( + // run = testexec.Run + stdout = testexec.Stdout +) diff --git a/tests/scenes/02_subdir/directory_option_test.go b/tests/scenes/02_subdir/directory_option_test.go new file mode 100644 index 0000000..9733af4 --- /dev/null +++ b/tests/scenes/02_subdir/directory_option_test.go @@ -0,0 +1,26 @@ +package basic + +import ( + "testing" + + "github.com/akm/git-exec/core" + "github.com/akm/git-exec/tests/testground" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDirectoryOption(t *testing.T) { + defer testground.Setup(t)() + + defer testground.AssertStringNotChanged(t, testground.GitDiff)() + + opts := *core.DefaultOptions + opts.Directory = "sub1" + err := core.Run(&opts, []string{"make", "add-one", "parent-add-two"}) + require.NoError(t, err) + + commitMessage := stdout(t, "git", "log", "-1", "--pretty=%B") + assert.Equal(t, `đŸ€– [02_subdir] $ make add-one parent-add-two + +`, commitMessage) +} diff --git a/tests/scenes/02_subdir/sub1/Makefile b/tests/scenes/02_subdir/sub1/Makefile new file mode 100644 index 0000000..cfe7206 --- /dev/null +++ b/tests/scenes/02_subdir/sub1/Makefile @@ -0,0 +1,13 @@ +WORK_FILE=work.txt + +.PHONY: add-one +add-one: + @echo "One" >> $(WORK_FILE) + +.PHONY: add-two +add-two: + @echo "Two" >> $(WORK_FILE) + +.PHONY: parent-% +parent-%: + @$(MAKE) -C .. $* From fae19eae37ab88b5fdf84e1345bb815d69b522ab Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 17:13:15 +0900 Subject: [PATCH 22/25] =?UTF-8?q?=E2=9C=85=20Don't=20run=20git=20config=20?= =?UTF-8?q?--global=20if=20git=20config=20file=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testground/setup.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/testground/setup.go b/tests/testground/setup.go index 6bcbffe..b0a8ba5 100644 --- a/tests/testground/setup.go +++ b/tests/testground/setup.go @@ -2,6 +2,7 @@ package testground import ( "os" + "path/filepath" "testing" "github.com/akm/git-exec/testdir" @@ -12,12 +13,13 @@ func Setup(t *testing.T) func() { t.Helper() if os.Getenv("GITHUB_ACTIONS") == "true" { - // git config --global user.email "foo@example.com" - // git config --global user.name "Foo Bar" - // git config --global init.defaultBranch main - testexec.Run(t, "git", "config", "--global", "user.email", "foo@example.com") - testexec.Run(t, "git", "config", "--global", "user.name", "Foo Bar") - testexec.Run(t, "git", "config", "--global", "init.defaultBranch", "main") + gitconfigPath := filepath.Join(os.Getenv("HOME"), ".gitconfig") + gitConfigPath := filepath.Join(os.Getenv("HOME"), ".config", "git", "config") + if !fileExists(gitconfigPath) && !fileExists(gitConfigPath) { + testexec.Run(t, "git", "config", "--global", "user.email", "foo@example.com") + testexec.Run(t, "git", "config", "--global", "user.name", "Foo Bar") + testexec.Run(t, "git", "config", "--global", "init.defaultBranch", "main") + } } // Suppress make's output @@ -30,3 +32,8 @@ func Setup(t *testing.T) func() { return r } + +func fileExists(path string) bool { + _, err := os.Stat(path) + return err == nil +} From 06880d5552576b9f416c41f5f2553d8a8d1889ee Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 17:23:57 +0900 Subject: [PATCH 23/25] =?UTF-8?q?=E2=9C=85=20Setup=20git=20before=20runnin?= =?UTF-8?q?g=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 5 +++++ tests/testground/setup.go | 16 ---------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0740e43..8b4298f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,5 +36,10 @@ jobs: - name: lint run: make lint + - name: setup git + run: git config --global user.email foo@example.com && \ + git config --global user.name "Foo Bar" && \ + git config --global init.defaultBranch main + - name: test run: make test diff --git a/tests/testground/setup.go b/tests/testground/setup.go index b0a8ba5..44cd1f3 100644 --- a/tests/testground/setup.go +++ b/tests/testground/setup.go @@ -2,7 +2,6 @@ package testground import ( "os" - "path/filepath" "testing" "github.com/akm/git-exec/testdir" @@ -12,16 +11,6 @@ import ( func Setup(t *testing.T) func() { t.Helper() - if os.Getenv("GITHUB_ACTIONS") == "true" { - gitconfigPath := filepath.Join(os.Getenv("HOME"), ".gitconfig") - gitConfigPath := filepath.Join(os.Getenv("HOME"), ".config", "git", "config") - if !fileExists(gitconfigPath) && !fileExists(gitConfigPath) { - testexec.Run(t, "git", "config", "--global", "user.email", "foo@example.com") - testexec.Run(t, "git", "config", "--global", "user.name", "Foo Bar") - testexec.Run(t, "git", "config", "--global", "init.defaultBranch", "main") - } - } - // Suppress make's output os.Setenv("MAKEFLAGS", "--no-print-directory") @@ -32,8 +21,3 @@ func Setup(t *testing.T) func() { return r } - -func fileExists(path string) bool { - _, err := os.Stat(path) - return err == nil -} From 6d60b9e91802a4a2a0b6419f92d6b6211696b12d Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 17:49:15 +0900 Subject: [PATCH 24/25] =?UTF-8?q?=F0=9F=94=A8=20Remove=20new=20lines=20in?= =?UTF-8?q?=20setup=20git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b4298f..1a78f4d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,9 +37,7 @@ jobs: run: make lint - name: setup git - run: git config --global user.email foo@example.com && \ - git config --global user.name "Foo Bar" && \ - git config --global init.defaultBranch main + run: git config --global user.email foo@example.com && git config --global user.name "Foo Bar" && git config --global init.defaultBranch main - name: test run: make test From a2951a20616307a9ed635173227a97340b9db509 Mon Sep 17 00:00:00 2001 From: akm Date: Sun, 8 Dec 2024 17:55:43 +0900 Subject: [PATCH 25/25] =?UTF-8?q?=F0=9F=94=A7=20Use=20multi=20lines=20YAML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1a78f4d..ce0e3fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,10 @@ jobs: run: make lint - name: setup git - run: git config --global user.email foo@example.com && git config --global user.name "Foo Bar" && git config --global init.defaultBranch main + run: |- + git config --global user.email foo@example.com && \ + git config --global user.name "Foo Bar" && \ + git config --global init.defaultBranch main - name: test run: make test