github.com/jkawamoto/roadie-azure@v0.3.5/roadie/archive_test.go (about)

     1  //
     2  // roadie/archive_test.go
     3  //
     4  // Copyright (c) 2017 Junpei Kawamoto
     5  //
     6  // This file is part of Roadie Azure.
     7  //
     8  // Roadie Azure is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // Roadie Azure is distributed in the hope that it will be useful,
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU General Public License
    19  // along with Roadie Azure. If not, see <http://www.gnu.org/licenses/>.
    20  //
    21  
    22  package roadie
    23  
    24  import (
    25  	"context"
    26  	"io/ioutil"
    27  	"log"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  )
    32  
    33  func TestExpandTarball(t *testing.T) {
    34  
    35  	fp, err := os.Open("archive_test.tar")
    36  	if err != nil {
    37  		t.Fatalf("cannot open %v: %v", "archive_test.tar", err)
    38  	}
    39  	defer fp.Close()
    40  
    41  	dir, err := ioutil.TempDir("", "")
    42  	if err != nil {
    43  		t.Fatalf("cannot create a temporary directory: %v", err)
    44  	}
    45  	defer os.RemoveAll(dir)
    46  
    47  	expander := NewExpander(log.New(ioutil.Discard, "", log.Lshortfile))
    48  	err = expander.ExpandTarball(context.Background(), fp, dir)
    49  	if err != nil {
    50  		t.Fatalf("ExpandTarball returns an error: %v", err)
    51  	}
    52  
    53  	// archive_test.tar
    54  	// ├── abc.txt
    55  	// ├── empty
    56  	// └── folder
    57  	//     └── def.txt
    58  	for _, expect := range []string{"abc.txt", "folder/def.txt"} {
    59  		var body, original []byte
    60  		body, err = ioutil.ReadFile(filepath.Join(dir, expect))
    61  		if err != nil {
    62  			t.Fatalf("ReadFile returns an error: %v", err)
    63  		}
    64  		original, err = ioutil.ReadFile(filepath.Join("../data", expect))
    65  		if err != nil {
    66  			t.Fatalf("ReadFile returns an error: %v", err)
    67  		}
    68  		if string(body) != string(original) {
    69  			t.Errorf("the file body is %q, want %q", string(body), string(original))
    70  		}
    71  	}
    72  
    73  	info, err := os.Stat(filepath.Join(dir, "empty"))
    74  	if err != nil {
    75  		t.Fatalf("Stat returns an error: %v", err)
    76  	}
    77  	if !info.IsDir() {
    78  		t.Error("expanded folder empty is not a directory")
    79  	}
    80  
    81  }
    82  
    83  func TestExpandZip(t *testing.T) {
    84  
    85  	fp, err := os.Open("archive_test.zip")
    86  	if err != nil {
    87  		t.Fatalf("cannot open %v: %v", "archive_test.zip", err)
    88  	}
    89  	defer fp.Close()
    90  
    91  	dir, err := ioutil.TempDir("", "")
    92  	if err != nil {
    93  		t.Fatalf("cannot create a temporary directory: %v", err)
    94  	}
    95  	defer os.RemoveAll(dir)
    96  
    97  	expander := NewExpander(log.New(ioutil.Discard, "", log.Lshortfile))
    98  	err = expander.ExpandZip(context.Background(), fp, dir)
    99  	if err != nil {
   100  		t.Fatalf("ExpandZip returns an error: %v", err)
   101  	}
   102  
   103  	// archive_test.tar
   104  	// ├── abc.txt
   105  	// ├── empty
   106  	// └── folder
   107  	//     └── def.txt
   108  	for _, expect := range []string{"abc.txt", "folder/def.txt"} {
   109  		var body, original []byte
   110  		body, err = ioutil.ReadFile(filepath.Join(dir, expect))
   111  		if err != nil {
   112  			t.Fatalf("ReadFile returns an error: %v", err)
   113  		}
   114  		original, err = ioutil.ReadFile(filepath.Join("../data", expect))
   115  		if err != nil {
   116  			t.Fatalf("ReadFile returns an error: %v", err)
   117  		}
   118  		if string(body) != string(original) {
   119  			t.Errorf("the file body is %q, want %q", string(body), string(original))
   120  		}
   121  	}
   122  
   123  	info, err := os.Stat(filepath.Join(dir, "empty"))
   124  	if err != nil {
   125  		t.Fatalf("Stat returns an error: %v", err)
   126  	}
   127  	if !info.IsDir() {
   128  		t.Error("expanded folder empty is not a directory")
   129  	}
   130  
   131  }