From 813e051711c35c585e14c24667ae04c73cde9248 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 09:28:30 +0200 Subject: [PATCH 1/3] chore: add Makefile with test command --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..456d79e --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +GOPATH=$(shell go env GOPATH) + +.PHONY: test +test: + @echo "==> Running tests" + go test -v ./... From cfeb21434aa3eba7fd3e201514bcc43f44193fc0 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 15:34:38 +0200 Subject: [PATCH 2/3] feat: add repository logic and unit tests --- repository/filesystem.go | 93 +++++++++++++++++++++++++++++++++++ repository/filesystem_test.go | 73 +++++++++++++++++++++++++++ types/repository.go | 15 ++++++ 3 files changed, 181 insertions(+) create mode 100644 repository/filesystem.go create mode 100644 repository/filesystem_test.go create mode 100644 types/repository.go diff --git a/repository/filesystem.go b/repository/filesystem.go new file mode 100644 index 0000000..02eccd6 --- /dev/null +++ b/repository/filesystem.go @@ -0,0 +1,93 @@ +package repository + +import ( + "os" + "io" + "io/ioutil" + "fmt" + "log" + "path" + cid "github.com/ipfs/go-cid" + mh "github.com/multiformats/go-multihash" + mbase "github.com/multiformats/go-multibase" +) + +// FileSystemRepository is a repository implementation which uses a +// file system to gets and store the data +type FileSystemRepository struct { + FileMode os.FileMode + Directory string +} + +// NewFileSystemRepository constructor +func NewFileSystemRepository(dir string) (*FileSystemRepository, error) { + if err := makeDirectoryIfDoesntExist(dir); err != nil { + return nil, fmt.Errorf("Failed to initialize repository the directory %s can't be created", dir) + } + + return &FileSystemRepository{ + FileMode: 0644, + Directory: dir, + }, nil +} + +// Add create resource and returns a unique identifier +func (repo *FileSystemRepository) Add(reader io.Reader) (string, error) { + + hash := cid.Prefix{ + Version: 1, + Codec: cid.Raw, + MhType: mh.SHA2_256, + MhLength: -1, // default length + } + + buf, err := ioutil.ReadAll(reader) + if err != nil { + return "", err; + } + + cid, err := hash.Sum(buf) + if err != nil { + return "", err; + } + + scid, err := cid.StringOfBase(mbase.Base58BTC); + if err != nil { + return "", err; + } + + fullpath := getFullpathFilename(repo.Directory, scid) + if err := ioutil.WriteFile(fullpath, buf, repo.FileMode); err != nil { + return "", err; + } + + return scid, nil +} + + +func (repo *FileSystemRepository) Get(scid string) (io.Reader, error) { + return os.Open(getFullpathFilename(repo.Directory, scid)) +} + +func (repo *FileSystemRepository) Remove(scid string) error { + return os.Remove(getFullpathFilename(repo.Directory, scid)) +} + +func getFullpathFilename(dir string, scid string) string { + return path.Join(dir, scid) +} + +// Checks if directory exists and if not then +// create it recusrsively like mkdir -p +func makeDirectoryIfDoesntExist(dir string) error { + _, err := os.Stat(dir) + if err == nil { + log.Printf("FileSystemRepository the directory \"%s\" already exists ignoring creation", dir) + return nil + } + err = os.MkdirAll(dir, os.ModePerm) + if err == nil { + log.Printf("FileSystemRepository root directory created! %s", dir) + } + return err +} \ No newline at end of file diff --git a/repository/filesystem_test.go b/repository/filesystem_test.go new file mode 100644 index 0000000..23b96eb --- /dev/null +++ b/repository/filesystem_test.go @@ -0,0 +1,73 @@ +package repository + +import ( + "io/ioutil" + "os" + "strings" + "testing" + "bytes" + "fmt" + "bufio" + "path" + "github.com/lot-sh/core/types" +) + +var ( + repo types.Repository + directory = path.Join(os.TempDir(), "lotsh/lib") + filecontent = "This is a test" + hash = "zb2rhk5zU8kdMPiPKgdxjhXTxs7KBxaXFqDhK2sBr5xQckX5f" + err error +) + +func TestErrorWhenInitialize(t *testing.T) { + prohibitedDir := "/test" + repo, err = NewFileSystemRepository(prohibitedDir) + if err == nil { + t.Errorf("Error must be raised but got nil") + } + actual := err.Error() + expected := fmt.Sprintf("Failed to initialize repository the directory %s can't be created", prohibitedDir) + if !strings.Contains(actual, expected) { + t.Errorf("Missmatch content \n-> Actual: %s \n-> Expected: %s", actual, expected) + } +} + +func TestInitializeWithoutErrors(t *testing.T) { + repo, err = NewFileSystemRepository(directory) + if err != nil { + t.Error(err) + } +} + +func TestRepositoryAdd(t *testing.T) { + buf := bytes.NewBufferString(filecontent) + actual, err := repo.Add(bufio.NewReader(buf)); + expected := hash + if err != nil { + t.Fatalf("Test failed in create resource; %s\n", err) + } + + if !strings.Contains(string(actual), expected) { + t.Errorf("Missmatch content \n-> Actual: %s \n-> Expected: %s", actual, expected) + } +} + +func TestRepositoryGet(t *testing.T) { + reader, err := repo.Get(hash); + actual, err := ioutil.ReadAll(reader) + expected := filecontent + if err != nil { + t.Logf("Test failed in get resource; %s\n", err) + } + + if !strings.Contains(string(actual), expected) { + t.Errorf("Missmatch content \n-> Actual: %s \n-> Expected: %s", actual, expected) + } +} + +func TestRepositoryRemove(t *testing.T) { + if err := repo.Remove(hash); err != nil { + t.Fatalf("Test failed when remove resource: %s\n", err) + } +} \ No newline at end of file diff --git a/types/repository.go b/types/repository.go new file mode 100644 index 0000000..3c4d5d7 --- /dev/null +++ b/types/repository.go @@ -0,0 +1,15 @@ +package types + +import ( + "io" +) + +//Repository provides access to the resource storage +type Repository interface { + // Get io.Reader from the resource storage by CID + Get(scid string) (io.Reader, error) + // Remove and existing resource using CID + Remove(scid string) error + // Add a resource with the CID as name generated from the content of io.Reader + Add(reader io.Reader) (string, error) +} \ No newline at end of file From bc9b34ce926dcb4893d4e2c75100f7e3c90fe639 Mon Sep 17 00:00:00 2001 From: "Miguel Osorio @Kelvur" Date: Thu, 18 Jun 2020 23:57:02 +0200 Subject: [PATCH 3/3] style: add gofmt to this branch --- main.go | 58 +++++++++++++++++------------------ repository/filesystem.go | 44 +++++++++++++------------- repository/filesystem_test.go | 23 +++++++------- types/repository.go | 2 +- 4 files changed, 64 insertions(+), 63 deletions(-) diff --git a/main.go b/main.go index 5408054..12d55c1 100644 --- a/main.go +++ b/main.go @@ -1,43 +1,43 @@ package core import ( - "bytes" - "io" - "log" - "os" + "bytes" + "io" + "log" + "os" - "github.com/ulikunitz/xz" - // "syscall/js" + "github.com/ulikunitz/xz" + // "syscall/js" ) func Compress() { - const text = "The quick brown fox jumps over the lazy dog.\n" - var buf bytes.Buffer - // compress text - w, err := xz.NewWriter(&buf) - if err != nil { - log.Fatalf("xz.NewWriter error %s", err) - } - if _, err := io.WriteString(w, text); err != nil { - log.Fatalf("WriteString error %s", err) - } - if err := w.Close(); err != nil { - log.Fatalf("w.Close error %s", err) - } - // decompress buffer and write output to stdout - r, err := xz.NewReader(&buf) - if err != nil { - log.Fatalf("NewReader error %s", err) - } - if _, err = io.Copy(os.Stdout, r); err != nil { - log.Fatalf("io.Copy error %s", err) - } + const text = "The quick brown fox jumps over the lazy dog.\n" + var buf bytes.Buffer + // compress text + w, err := xz.NewWriter(&buf) + if err != nil { + log.Fatalf("xz.NewWriter error %s", err) + } + if _, err := io.WriteString(w, text); err != nil { + log.Fatalf("WriteString error %s", err) + } + if err := w.Close(); err != nil { + log.Fatalf("w.Close error %s", err) + } + // decompress buffer and write output to stdout + r, err := xz.NewReader(&buf) + if err != nil { + log.Fatalf("NewReader error %s", err) + } + if _, err = io.Copy(os.Stdout, r); err != nil { + log.Fatalf("io.Copy error %s", err) + } } // var c = make(chan struct{}) // func main() { - + // js.Global().Set("myexport", "juas") // <-c -// } \ No newline at end of file +// } diff --git a/repository/filesystem.go b/repository/filesystem.go index 02eccd6..46b4980 100644 --- a/repository/filesystem.go +++ b/repository/filesystem.go @@ -1,21 +1,22 @@ package repository import ( - "os" + "fmt" "io" "io/ioutil" - "fmt" "log" + "os" "path" + cid "github.com/ipfs/go-cid" - mh "github.com/multiformats/go-multihash" mbase "github.com/multiformats/go-multibase" + mh "github.com/multiformats/go-multihash" ) -// FileSystemRepository is a repository implementation which uses a +// FileSystemRepository is a repository implementation which uses a // file system to gets and store the data type FileSystemRepository struct { - FileMode os.FileMode + FileMode os.FileMode Directory string } @@ -24,9 +25,9 @@ func NewFileSystemRepository(dir string) (*FileSystemRepository, error) { if err := makeDirectoryIfDoesntExist(dir); err != nil { return nil, fmt.Errorf("Failed to initialize repository the directory %s can't be created", dir) } - + return &FileSystemRepository{ - FileMode: 0644, + FileMode: 0644, Directory: dir, }, nil } @@ -35,41 +36,40 @@ func NewFileSystemRepository(dir string) (*FileSystemRepository, error) { func (repo *FileSystemRepository) Add(reader io.Reader) (string, error) { hash := cid.Prefix{ - Version: 1, - Codec: cid.Raw, - MhType: mh.SHA2_256, + Version: 1, + Codec: cid.Raw, + MhType: mh.SHA2_256, MhLength: -1, // default length } buf, err := ioutil.ReadAll(reader) if err != nil { - return "", err; + return "", err } cid, err := hash.Sum(buf) - if err != nil { - return "", err; + if err != nil { + return "", err } - scid, err := cid.StringOfBase(mbase.Base58BTC); - if err != nil { - return "", err; + scid, err := cid.StringOfBase(mbase.Base58BTC) + if err != nil { + return "", err } fullpath := getFullpathFilename(repo.Directory, scid) if err := ioutil.WriteFile(fullpath, buf, repo.FileMode); err != nil { - return "", err; + return "", err } return scid, nil } - -func (repo *FileSystemRepository) Get(scid string) (io.Reader, error) { +func (repo *FileSystemRepository) Get(scid string) (io.Reader, error) { return os.Open(getFullpathFilename(repo.Directory, scid)) } -func (repo *FileSystemRepository) Remove(scid string) error { +func (repo *FileSystemRepository) Remove(scid string) error { return os.Remove(getFullpathFilename(repo.Directory, scid)) } @@ -77,7 +77,7 @@ func getFullpathFilename(dir string, scid string) string { return path.Join(dir, scid) } -// Checks if directory exists and if not then +// Checks if directory exists and if not then // create it recusrsively like mkdir -p func makeDirectoryIfDoesntExist(dir string) error { _, err := os.Stat(dir) @@ -90,4 +90,4 @@ func makeDirectoryIfDoesntExist(dir string) error { log.Printf("FileSystemRepository root directory created! %s", dir) } return err -} \ No newline at end of file +} diff --git a/repository/filesystem_test.go b/repository/filesystem_test.go index 23b96eb..c3a187e 100644 --- a/repository/filesystem_test.go +++ b/repository/filesystem_test.go @@ -1,23 +1,24 @@ package repository import ( + "bufio" + "bytes" + "fmt" "io/ioutil" "os" + "path" "strings" "testing" - "bytes" - "fmt" - "bufio" - "path" + "github.com/lot-sh/core/types" ) var ( - repo types.Repository - directory = path.Join(os.TempDir(), "lotsh/lib") + repo types.Repository + directory = path.Join(os.TempDir(), "lotsh/lib") filecontent = "This is a test" - hash = "zb2rhk5zU8kdMPiPKgdxjhXTxs7KBxaXFqDhK2sBr5xQckX5f" - err error + hash = "zb2rhk5zU8kdMPiPKgdxjhXTxs7KBxaXFqDhK2sBr5xQckX5f" + err error ) func TestErrorWhenInitialize(t *testing.T) { @@ -42,7 +43,7 @@ func TestInitializeWithoutErrors(t *testing.T) { func TestRepositoryAdd(t *testing.T) { buf := bytes.NewBufferString(filecontent) - actual, err := repo.Add(bufio.NewReader(buf)); + actual, err := repo.Add(bufio.NewReader(buf)) expected := hash if err != nil { t.Fatalf("Test failed in create resource; %s\n", err) @@ -54,7 +55,7 @@ func TestRepositoryAdd(t *testing.T) { } func TestRepositoryGet(t *testing.T) { - reader, err := repo.Get(hash); + reader, err := repo.Get(hash) actual, err := ioutil.ReadAll(reader) expected := filecontent if err != nil { @@ -70,4 +71,4 @@ func TestRepositoryRemove(t *testing.T) { if err := repo.Remove(hash); err != nil { t.Fatalf("Test failed when remove resource: %s\n", err) } -} \ No newline at end of file +} diff --git a/types/repository.go b/types/repository.go index 3c4d5d7..daca294 100644 --- a/types/repository.go +++ b/types/repository.go @@ -12,4 +12,4 @@ type Repository interface { Remove(scid string) error // Add a resource with the CID as name generated from the content of io.Reader Add(reader io.Reader) (string, error) -} \ No newline at end of file +}