From 7f7c192470056b91decbbbf812a1af9efd313e10 Mon Sep 17 00:00:00 2001 From: Ferenc Szabo Date: Sun, 9 Aug 2020 18:48:18 +0200 Subject: [PATCH] Use non-local tempdirs in import export tests Given a project uses Go modules and has `universal-translator` as a dependency, then `go test all` will fail with permission errors. Cause: The Go tool runs tests under `pkg/mod` and the packages are readonly. The failing tests tried to create directories there. Fixes #24 Signed-off-by: Ferenc Szabo --- import_export_test.go | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/import_export_test.go b/import_export_test.go index 5dbae2d..1e721b5 100644 --- a/import_export_test.go +++ b/import_export_test.go @@ -2,6 +2,7 @@ package ut import ( "fmt" + "io/ioutil" "path/filepath" "testing" @@ -98,8 +99,7 @@ func TestExportImportBasic(t *testing.T) { } } - dirname := "testdata/translations" - defer os.RemoveAll(dirname) + dirname := tempDir(t) err := uni.Export(FormatJSON, dirname) if err != nil { @@ -227,8 +227,7 @@ func TestExportImportCardinal(t *testing.T) { } } - dirname := "testdata/translations" - defer os.RemoveAll(dirname) + dirname := tempDir(t) err := uni.Export(FormatJSON, dirname) if err != nil { @@ -364,8 +363,7 @@ func TestExportImportOrdinal(t *testing.T) { } } - dirname := "testdata/translations" - defer os.RemoveAll(dirname) + dirname := tempDir(t) err := uni.Export(FormatJSON, dirname) if err != nil { @@ -527,8 +525,7 @@ func TestExportImportRange(t *testing.T) { } } - dirname := "testdata/translations" - defer os.RemoveAll(dirname) + dirname := tempDir(t) err := uni.Export(FormatJSON, dirname) if err != nil { @@ -765,25 +762,41 @@ func TestBadExport(t *testing.T) { t.Fatalf("Expected '%t' Got '%t'", true, found) } - dirname := "testdata/readonly" - err := os.Mkdir(dirname, 0444) + dirname := tempDir(t) + readonly := os.FileMode(0444) + err := os.Chmod(dirname, readonly) if err != nil { t.Fatalf("Expected '%v' Got '%s'", nil, err) } - defer os.RemoveAll(dirname) en.Add("day", "this is a day", false) - expected := "open testdata/readonly/en.json: permission denied" + expected := "open " + dirname + "/en.json: permission denied" err = uni.Export(FormatJSON, dirname) if err == nil || err.Error() != expected { t.Fatalf("Expected '%s' Got '%s'", expected, err) } // test exporting into directory inside readonly directory - expected = "stat testdata/readonly/inner: permission denied" + expected = "stat " + dirname + "/inner: permission denied" err = uni.Export(FormatJSON, filepath.Join(dirname, "inner")) if err == nil || err.Error() != expected { t.Fatalf("Expected '%s' Got '%s'", expected, err) } } + +func tempDir(t *testing.T) string { + t.Helper() + + result, err := ioutil.TempDir("", "translations-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := os.RemoveAll(result); err != nil { + t.Errorf("tempdir cleanup: %v", err) + } + }) + + return result +}