From 5a562becc38ad9f1575c8fa417dfa20a197dd2f9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 1 Jun 2021 18:20:19 +0200 Subject: [PATCH 1/3] Added GUnzip function --- _testdata/test.txt | 20 ++++++++++++ _testdata/test.txt.gz | Bin 0 -> 411 bytes gzip.go | 70 ++++++++++++++++++++++++++++++++++++++++++ gzip_test.go | 58 ++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 _testdata/test.txt create mode 100644 _testdata/test.txt.gz create mode 100644 gzip.go create mode 100644 gzip_test.go diff --git a/_testdata/test.txt b/_testdata/test.txt new file mode 100644 index 0000000..d3ded99 --- /dev/null +++ b/_testdata/test.txt @@ -0,0 +1,20 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. + diff --git a/_testdata/test.txt.gz b/_testdata/test.txt.gz new file mode 100644 index 0000000000000000000000000000000000000000..e75120aef293a5d686de117d35f37b59d821e907 GIT binary patch literal 411 zcmb2|=HOTtzAb@)xg@o?M6aZxgyHQZ!#rg}9@pP9rSEo9xOPuzX7@_%gh@toR1amHo?)WOb&KoxhZ#qWBN!wq`&(Un zRA-$&8^*!?sr8v*#nyvcugu%vdh_f(=c>wjYpykc(+hLWm|DNF`Y$=#$F)gdrGv(r z|I2LL;sROpcfYjl$i_@% literal 0 HcmV?d00001 diff --git a/gzip.go b/gzip.go new file mode 100644 index 0000000..b1982ff --- /dev/null +++ b/gzip.go @@ -0,0 +1,70 @@ +/* + * This file is part of PathsHelper library. + * + * Copyright 2021 Arduino AG (http://www.arduino.cc/) + * + * PathsHelper library is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package paths + +import ( + "compress/gzip" + "io" + + "github.com/pkg/errors" +) + +// GZip compress src with gzip and writes the compressed file on dst +//func GZip(src, dst *Path) error { +// return errors.New("gzip unimplemented") +//} + +// GUnzip decompress src with gzip and writes the uncompressed file on dst +func GUnzip(src, dest *Path) error { + gzIn, err := src.Open() + if err != nil { + return errors.Wrap(err, "opening "+src.String()) + } + defer gzIn.Close() + + in, err := gzip.NewReader(gzIn) + if err != nil { + return errors.Wrap(err, "decoding "+dest.String()) + } + defer in.Close() + + out, err := dest.Create() + if err != nil { + return errors.Wrap(err, "creating "+dest.String()) + } + defer out.Close() + + _, err = io.Copy(out, in) + if err != nil { + return errors.Wrap(err, "uncompressing "+dest.String()) + } + + return nil +} diff --git a/gzip_test.go b/gzip_test.go new file mode 100644 index 0000000..d102488 --- /dev/null +++ b/gzip_test.go @@ -0,0 +1,58 @@ +/* + * This file is part of PathsHelper library. + * + * Copyright 2021 Arduino AG (http://www.arduino.cc/) + * + * PathsHelper library is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ + +package paths + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGzipGunzip(t *testing.T) { + zipped := New("_testdata", "test.txt.gz") + unzipped := New("_testdata", "test.txt") + + tmp, err := MkTempDir("", "") + require.NoError(t, err) + defer tmp.RemoveAll() + + // Test decoding + decoded := tmp.Join("test") + err = GUnzip(zipped, decoded) + require.NoError(t, err) + d, err := decoded.ReadFile() + require.NoError(t, err) + u, err := unzipped.ReadFile() + require.NoError(t, err) + require.Equal(t, u, d) + + // Test encoding + // TODO +} From 6afa41011bdac562d9a2a94cd93aaa3da18d45eb Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 1 Jun 2021 18:25:15 +0200 Subject: [PATCH 2/3] fixed tests... oops --- paths_test.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/paths_test.go b/paths_test.go index 09ba8f2..c3230d5 100644 --- a/paths_test.go +++ b/paths_test.go @@ -259,7 +259,7 @@ func TestReadDirRecursive(t *testing.T) { list, err := testPath.ReadDirRecursive() require.NoError(t, err) - require.Len(t, list, 14) + require.Len(t, list, 16) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) @@ -275,6 +275,8 @@ func TestReadDirRecursive(t *testing.T) { pathEqualsTo(t, "_testdata/symlinktofolder/file3", list[11]) pathEqualsTo(t, "_testdata/symlinktofolder/subfolder", list[12]) pathEqualsTo(t, "_testdata/symlinktofolder/subfolder/file4", list[13]) + pathEqualsTo(t, "_testdata/test.txt", list[14]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[15]) } func TestFilterDirs(t *testing.T) { @@ -282,12 +284,14 @@ func TestFilterDirs(t *testing.T) { list, err := testPath.ReadDir() require.NoError(t, err) - require.Len(t, list, 4) + require.Len(t, list, 6) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) pathEqualsTo(t, "_testdata/folder", list[2]) pathEqualsTo(t, "_testdata/symlinktofolder", list[3]) + pathEqualsTo(t, "_testdata/test.txt", list[4]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[5]) list.FilterDirs() require.Len(t, list, 2) @@ -300,17 +304,21 @@ func TestFilterOutDirs(t *testing.T) { list, err := testPath.ReadDir() require.NoError(t, err) - require.Len(t, list, 4) + require.Len(t, list, 6) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) pathEqualsTo(t, "_testdata/folder", list[2]) pathEqualsTo(t, "_testdata/symlinktofolder", list[3]) + pathEqualsTo(t, "_testdata/test.txt", list[4]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[5]) list.FilterOutDirs() - require.Len(t, list, 2) + require.Len(t, list, 4) pathEqualsTo(t, "_testdata/anotherFile", list[0]) pathEqualsTo(t, "_testdata/file", list[1]) + pathEqualsTo(t, "_testdata/test.txt", list[2]) + pathEqualsTo(t, "_testdata/test.txt.gz", list[3]) } func TestEquivalentPaths(t *testing.T) { From e27cc55b147acf6d925531cc1f3d7854c7ffec45 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 1 Jun 2021 18:26:46 +0200 Subject: [PATCH 3/3] Fixed error msg --- gzip.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gzip.go b/gzip.go index b1982ff..3a841ae 100644 --- a/gzip.go +++ b/gzip.go @@ -51,7 +51,7 @@ func GUnzip(src, dest *Path) error { in, err := gzip.NewReader(gzIn) if err != nil { - return errors.Wrap(err, "decoding "+dest.String()) + return errors.Wrap(err, "decoding "+src.String()) } defer in.Close()