Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use non-local tempdirs in import export tests #25

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions import_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ut

import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}