diff --git a/examples/testing/testing_bench_profile/.gitignore b/examples/testing/bench_profile/.gitignore similarity index 100% rename from examples/testing/testing_bench_profile/.gitignore rename to examples/testing/bench_profile/.gitignore diff --git a/examples/testing/testing_bench_profile/Makefile b/examples/testing/bench_profile/Makefile similarity index 100% rename from examples/testing/testing_bench_profile/Makefile rename to examples/testing/bench_profile/Makefile diff --git a/examples/testing/testing_bench_profile/README.md b/examples/testing/bench_profile/README.md similarity index 100% rename from examples/testing/testing_bench_profile/README.md rename to examples/testing/bench_profile/README.md diff --git a/examples/testing/testing_bench_profile/lib.go b/examples/testing/bench_profile/lib.go similarity index 100% rename from examples/testing/testing_bench_profile/lib.go rename to examples/testing/bench_profile/lib.go diff --git a/examples/testing/testing_bench_profile/lib_test.go b/examples/testing/bench_profile/lib_test.go similarity index 100% rename from examples/testing/testing_bench_profile/lib_test.go rename to examples/testing/bench_profile/lib_test.go diff --git a/examples/testing/testing_count/Taskfile.yml b/examples/testing/count/Taskfile.yml similarity index 100% rename from examples/testing/testing_count/Taskfile.yml rename to examples/testing/count/Taskfile.yml diff --git a/examples/testing/testing_count/lib._test.go b/examples/testing/count/lib._test.go similarity index 100% rename from examples/testing/testing_count/lib._test.go rename to examples/testing/count/lib._test.go diff --git a/examples/testing/testing_count/lib.go b/examples/testing/count/lib.go similarity index 100% rename from examples/testing/testing_count/lib.go rename to examples/testing/count/lib.go diff --git a/examples/testing/testing_pkg_coexist/Makefile b/examples/testing/package_names/Makefile similarity index 100% rename from examples/testing/testing_pkg_coexist/Makefile rename to examples/testing/package_names/Makefile diff --git a/examples/testing/testing_pkg_coexist/README.md b/examples/testing/package_names/README.md similarity index 100% rename from examples/testing/testing_pkg_coexist/README.md rename to examples/testing/package_names/README.md diff --git a/examples/testing/testing_pkg_coexist/a_test.go b/examples/testing/package_names/a_test.go similarity index 100% rename from examples/testing/testing_pkg_coexist/a_test.go rename to examples/testing/package_names/a_test.go diff --git a/examples/testing/testing_pkg_coexist/b_test.go b/examples/testing/package_names/b_test.go similarity index 100% rename from examples/testing/testing_pkg_coexist/b_test.go rename to examples/testing/package_names/b_test.go diff --git a/examples/testing/testing_short_feature/Makefile b/examples/testing/short_feature/Makefile similarity index 100% rename from examples/testing/testing_short_feature/Makefile rename to examples/testing/short_feature/Makefile diff --git a/examples/testing/testing_short_feature/Taskfile.yml b/examples/testing/short_feature/Taskfile.yml similarity index 100% rename from examples/testing/testing_short_feature/Taskfile.yml rename to examples/testing/short_feature/Taskfile.yml diff --git a/examples/testing/testing_short_feature/lib/lib.go b/examples/testing/short_feature/lib/lib.go similarity index 100% rename from examples/testing/testing_short_feature/lib/lib.go rename to examples/testing/short_feature/lib/lib.go diff --git a/examples/testing/testing_short_feature/lib/lib_test.go b/examples/testing/short_feature/lib/lib_test.go similarity index 83% rename from examples/testing/testing_short_feature/lib/lib_test.go rename to examples/testing/short_feature/lib/lib_test.go index 35355d35..a0447d6d 100644 --- a/examples/testing/testing_short_feature/lib/lib_test.go +++ b/examples/testing/short_feature/lib/lib_test.go @@ -3,7 +3,7 @@ package lib_test import ( "testing" - "github.com/devlights/try-golang/examples/testing/testing_short_feature/lib" + "github.com/devlights/try-golang/examples/testing/short_feature/lib" ) func TestAdd(t *testing.T) { diff --git a/examples/testing/testing_short_feature/main.go b/examples/testing/short_feature/main.go similarity index 100% rename from examples/testing/testing_short_feature/main.go rename to examples/testing/short_feature/main.go diff --git a/examples/testing/testing_shuffle/Taskfile.yml b/examples/testing/shuffle/Taskfile.yml similarity index 100% rename from examples/testing/testing_shuffle/Taskfile.yml rename to examples/testing/shuffle/Taskfile.yml diff --git a/examples/testing/testing_shuffle/lib_test.go b/examples/testing/shuffle/lib_test.go similarity index 100% rename from examples/testing/testing_shuffle/lib_test.go rename to examples/testing/shuffle/lib_test.go diff --git a/examples/testing/tempdir/Taskfile.yml b/examples/testing/tempdir/Taskfile.yml new file mode 100644 index 00000000..dc9dd0c6 --- /dev/null +++ b/examples/testing/tempdir/Taskfile.yml @@ -0,0 +1,8 @@ +# https://taskfile.dev + +version: '3' + +tasks: + default: + cmds: + - go test -v . diff --git a/examples/testing/tempdir/main_test.go b/examples/testing/tempdir/main_test.go new file mode 100644 index 00000000..e8c819d0 --- /dev/null +++ b/examples/testing/tempdir/main_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +var ( + tmpDir string +) + +func TestMain(m *testing.M) { + ret := m.Run() + + // テスト終了後に一時ディレクトリが削除されているか確認 + if tmpDir != "" { + _, err := os.Stat(tmpDir) + fmt.Printf("[teardown] os.Stat() ==> %v", err) + } + + os.Exit(ret) +} + +func TestTempDir(t *testing.T) { + // + // t.TempDir() にてテスト時に利用できる一時ディレクトリの + // パスが取得出来る。この一時ディレクトリはテスト時に作成されて + // テスト終了後に自動的に消去される。テスト時に手動で一時ディレクトリを + // 確保しておく手間が無くなるため、とても便利。 + // + tmpDir = t.TempDir() + t.Logf("tmpDir=%s", tmpDir) + + fi, err := os.Stat(tmpDir) + t.Logf("IsDir=%v, Name=%s, Err=%v", fi.IsDir(), fi.Name(), err) + + // ディレクトリが存在しているだけでは何なので + // 何かのファイルを書き込んでおく + p := filepath.Join(tmpDir, "hello.txt") + os.WriteFile(p, []byte("hello world"), 0777) + + fi, err = os.Stat(p) + t.Logf("IsDir=%v, Name=%s, Err=%v", fi.IsDir(), fi.Name(), err) + + // t.TempDir() の呼び出しは何回でも良い + // その度に、異なるディレクトリが返る + for i := 0; i < 10; i++ { + t.Logf("tmpDir=%s", t.TempDir()) + } +}