Skip to content
Snippets Groups Projects
ext_writeable.go 5.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Marc René Arns's avatar
    Marc René Arns committed
    package spec
    
    import (
    
    Marc René Arns's avatar
    Marc René Arns committed
    	"io"
    	"os"
    	"strings"
    
    Marc René Arns's avatar
    Marc René Arns committed
    	"testing"
    
    	"gitlab.com/golang-utils/fs"
    
    Marc René Arns's avatar
    Marc René Arns committed
    	"gitlab.com/golang-utils/fs/path"
    
    Marc René Arns's avatar
    Marc René Arns committed
    	"gitlab.com/golang-utils/spectest"
    )
    
    /*
    // Writeable is an interface for a filesystem, that expands on the Minimal interface
    // and adds the ability to write files and folders.
    type Writeable interface {
    	Minimal
    
    	// Write writes either a file or a folder to the given path with default permissions.
    	// If p is a directory, data is ignored.
    	// If recursive is set to true, all intermediate directories will be created if necessary.
    	// Write always overwrites an existing file. If you need to make sure that a file at the given
    	// path should not be overwritten, check with Exists(p) first.
    	// If p is a directory that already exists, nothing will be done and no error will be returned.
    	Write(p path.Relative, data io.ReadCloser, recursive bool) error
    }
    */
    
    
    Marc René Arns's avatar
    Marc René Arns committed
    func ExtWriteable(c Config, fn func(path.Absolute) fs.ExtWriteable) *spectest.Spec {
    	spec := spectest.NewSpec("ExtWriteable", "test spec for the ExtWriteable interface")
    
    Marc René Arns's avatar
    Marc René Arns committed
    	tests := &writeable{c, fn}
    
    Marc René Arns's avatar
    Marc René Arns committed
    	write := spectest.NewSpec("Write", "Write writes either a file or a folder to the given path with default permissions.")
    	write.AddTest("write file", tests.testWriteFile)
    
    Marc René Arns's avatar
    Marc René Arns committed
    
    	write.AddTest("overwrite file", func(t *testing.T) {
    
    	})
    
    	write.AddTest("write file recursive", func(t *testing.T) {
    
    	})
    
    	write.AddTest("write dir", func(t *testing.T) {
    
    	})
    
    	write.AddTest("overwrite dir", func(t *testing.T) {
    
    	})
    
    
    Marc René Arns's avatar
    Marc René Arns committed
    	write.AddTest("write dir recursive", tests.testMkdirAll)
    
    Marc René Arns's avatar
    Marc René Arns committed
    
    	spec.AddSubSpec(write)
    
    	return spec
    }
    
    Marc René Arns's avatar
    Marc René Arns committed
    
    type writeable struct {
    	conf Config
    	fn   func(path.Absolute) fs.ExtWriteable
    }
    
    func (fstest *writeable) testMkdirAll(t *testing.T) {
    
    	//	fmt.Println(path.MustWD().Join("tests/"))
    
    	if fstest.conf.isLocal {
    		os.RemoveAll("tests")
    		os.Mkdir("tests", 0755)
    
    		os.Mkdir("tests/with space/", 0755)
    
    Marc René Arns's avatar
    Marc René Arns committed
    	}
    
    	tests := []struct {
    		forlocal bool
    		fsroot   path.Absolute
    		dir      string
    	}{
    		{false, path.MustLocal("C:"), "testdir_c/d/e/"},
    		{false, path.MustLocal("C:"), "testdir_a/b/c/"},
    		{true, path.MustWD().Join("tests/"), "testdir_b/g/f/"},
    		{true, path.MustWD().Join("tests/"), "testdir_b/g/f/"},
    
    		{true, path.MustWD().Join("tests/with space/"), "testdir_b/g/f/"},
    		{true, path.MustWD().Join("tests/with space/"), "testdir b/a b/f/"},
    
    Marc René Arns's avatar
    Marc René Arns committed
    		{false, path.MustLocal("C:"), "testdir_g/g/f/"},
    
    		{false, path.MustLocal("C:/fh h/"), "testdir c/g a/f/"},
    
    Marc René Arns's avatar
    Marc René Arns committed
    	}
    
    	for i, test := range tests {
    
    		if fstest.conf.isLocal && !test.forlocal {
    			continue
    		}
    
    		fsys := fstest.fn(test.fsroot)
    		if fsys == nil {
    			continue
    		}
    
    		rel := path.Relative(test.dir)
    
    		if !path.IsDir(rel) {
    			t.Errorf("[%v] %T(%q): %q is not a dir", i, fsys, test.fsroot, test.dir)
    		}
    
    		err := fs.MkDirAll(fsys, rel)
    
    		if err != nil {
    			t.Fatalf("[%v] %T(%q): could not create dir %q: %v", i, fsys, test.fsroot, test.dir, err)
    		}
    
    		if !fsys.Exists(rel) {
    			t.Errorf("[%v] %T(%q): dir %q not created", i, fsys, test.fsroot, test.dir)
    		}
    
    	}
    
    }
    
    func (fstest *writeable) testWriteFile(t *testing.T) {
    	if fstest.conf.Unsupported.WritingFile {
    		t.Skip()
    		return
    	}
    
    	os.RemoveAll("tests")
    	os.Mkdir("tests", 0755)
    
    	os.Mkdir("tests/with space", 0755)
    
    Marc René Arns's avatar
    Marc René Arns committed
    
    	tests := []struct {
    		forLocal  bool
    		fsroot    path.Absolute
    		fileWrite string
    		data      string
    		fileRead  string
    	}{
    		{false, path.MustLocal("/memory/"), "testdir_a/b/c", "content", "testdir_a/b/c"},
    		{false, path.MustLocal("/map/"), "testdir_c/d/e", "content", "testdir_c/d/e"},
    		{true, path.MustWD().Join("tests/"), "f", "content", "f"},
    		{true, path.MustWD().Join("tests/"), "testdir_b/g/f", "content", "testdir_b/g/f"},
    
    		{true, path.MustWD().Join("tests/with space/"), "testdir_b/g/f", "content", "testdir_b/g/f"},
    		{false, path.MustLocal("C:/fh h/"), "testdir c/g a/f", "content", "testdir c/g a/f"},
    
    Marc René Arns's avatar
    Marc René Arns committed
    		//{"Dir-schon vorhanden", Dir("tests"), "testdir_b/g/f"},
    	}
    
    	for i, test := range tests {
    		if fstest.conf.isLocal && !test.forLocal {
    			continue
    		}
    
    		fsys := fstest.fn(test.fsroot)
    
    		if fsys == nil {
    			continue
    		}
    
    		file := path.Relative(test.fileWrite)
    		dir := path.Dir(file).Relative()
    
    		if dir.String() != "" && !fsys.Exists(dir) {
    			err := fs.MkDirAll(fsys, dir)
    
    			if err != nil {
    				t.Fatalf("[%v] %T(%q): could not create dir %q: %v", i, fsys, test.fsroot, dir, err)
    			}
    		}
    
    		rd := strings.NewReader(test.data)
    		err := fsys.Write(file, fs.ReadCloser(rd), false)
    
    		//	fmt.Printf("writing file %q\n", file)
    
    		if err != nil {
    			t.Fatalf("[%v] %T(%q): could not write file %q: %T %v", i, fsys, test.fsroot, test.fileWrite, err, err)
    		}
    
    		if !fsys.Exists(file) {
    			t.Errorf("[%v] %T(%q): file %q not created", i, fsys, test.fsroot, test.fileWrite)
    		}
    
    		if path.IsDir(file) {
    			t.Errorf("[%v] %T(%q): %q is a dir and not a file", i, fsys, test.fsroot, test.fileWrite)
    		}
    
    		file = path.Relative(test.fileRead)
    
    		if !fsys.Exists(file) {
    			t.Errorf("[%v] %T(%q): file %q not there", i, fsys, test.fsroot, test.fileRead)
    		}
    
    		if path.IsDir(file) {
    			t.Errorf("[%v] %T(%q): %q is a dir and not a file", i, fsys, test.fsroot, test.fileRead)
    		}
    
    		rd2, err := fsys.Reader(file)
    
    		if err != nil {
    			t.Fatalf("[%v] %T(%q): could not read from file %q: %v", i, fsys, test.fsroot, test.fileRead, err)
    		}
    
    		bt, err := io.ReadAll(rd2)
    
    		if err != nil {
    			t.Fatalf("[%v] %T(%q): could not readall from file %q: %v", i, fsys, test.fsroot, test.fileRead, err)
    		}
    
    		if got, want := string(bt), test.data; got != want {
    			t.Errorf("[%v] %T(%q) content of %q = %q // expected %q", i, fsys, test.fsroot, test.fileRead, got, want)
    		}
    	}
    
    }