github.com/jaypipes/ghw@v0.21.1/pkg/snapshot/unpack_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package snapshot_test
     8  
     9  import (
    10  	"errors"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/jaypipes/ghw/pkg/snapshot"
    16  )
    17  
    18  const (
    19  	testDataSnapshot = "testdata.tar.gz"
    20  )
    21  
    22  // nolint: gocyclo
    23  func TestUnpack(t *testing.T) {
    24  	root, err := snapshot.Unpack(testDataSnapshot)
    25  	if err != nil {
    26  		t.Fatalf("Expected nil err, but got %v", err)
    27  	}
    28  
    29  	verifyTestData(t, root)
    30  
    31  	err = snapshot.Cleanup(root)
    32  	if err != nil {
    33  		t.Fatalf("Expected nil err, but got %v", err)
    34  	}
    35  
    36  	if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
    37  		t.Fatalf("Expected %q to be gone, but still exists", root)
    38  	}
    39  }
    40  
    41  // nolint: gocyclo
    42  func TestUnpackInto(t *testing.T) {
    43  	testRoot, err := os.MkdirTemp("", "ghw-test-snapshot-*")
    44  	if err != nil {
    45  		t.Fatalf("Expected nil err, but got %v", err)
    46  	}
    47  
    48  	_, err = snapshot.UnpackInto(testDataSnapshot, testRoot, 0)
    49  	if err != nil {
    50  		t.Fatalf("Expected nil err, but got %v", err)
    51  	}
    52  
    53  	verifyTestData(t, testRoot)
    54  
    55  	// note that in real production code the caller will likely manage its
    56  	// snapshot root directory in a different way, here we call snapshot.Cleanup
    57  	// to clean up after ourselves more than to test it
    58  	err = snapshot.Cleanup(testRoot)
    59  	if err != nil {
    60  		t.Fatalf("Expected nil err, but got %v", err)
    61  	}
    62  
    63  	if _, err := os.Stat(testRoot); !errors.Is(err, os.ErrNotExist) {
    64  		t.Fatalf("Expected %q to be gone, but still exists", testRoot)
    65  	}
    66  }
    67  
    68  // nolint: gocyclo
    69  func TestUnpackIntoPresrving(t *testing.T) {
    70  	testRoot, err := os.MkdirTemp("", "ghw-test-snapshot-*")
    71  	if err != nil {
    72  		t.Fatalf("Expected nil err, but got %v", err)
    73  	}
    74  
    75  	err = os.WriteFile(filepath.Join(testRoot, "canary"), []byte(""), 0644)
    76  	if err != nil {
    77  		t.Fatalf("Expected nil err, but got %v", err)
    78  	}
    79  
    80  	_, err = snapshot.UnpackInto(testDataSnapshot, testRoot, snapshot.OwnTargetDirectory)
    81  	if err != nil {
    82  		t.Fatalf("Expected nil err, but got %v", err)
    83  	}
    84  
    85  	entries, err := os.ReadDir(testRoot)
    86  	if err != nil {
    87  		t.Fatalf("Expected nil err, but got %v", err)
    88  	}
    89  	if len(entries) != 1 {
    90  		t.Fatalf("Expected one entry in %q, but got %v", testRoot, entries)
    91  	}
    92  
    93  	canary := entries[0]
    94  	if canary.Name() != "canary" {
    95  		t.Fatalf("Expected entry %q, but got %q", "canary", canary.Name())
    96  	}
    97  
    98  	// note that in real production code the caller will likely manage its
    99  	// snapshot root directory in a different way, here we call snapshot.Cleanup
   100  	// to clean up after ourselves more than to test it
   101  	err = snapshot.Cleanup(testRoot)
   102  	if err != nil {
   103  		t.Fatalf("Expected nil err, but got %v", err)
   104  	}
   105  
   106  	if _, err := os.Stat(testRoot); !errors.Is(err, os.ErrNotExist) {
   107  		t.Fatalf("Expected %q to be gone, but still exists", testRoot)
   108  	}
   109  }
   110  
   111  func verifyTestData(t *testing.T, root string) {
   112  	verifyFileContent(t, filepath.Join(root, "ghw-test-0"), "ghw-test-0\n")
   113  	verifyFileContent(t, filepath.Join(root, "ghw-test-1"), "ghw-test-1\n")
   114  	verifyFileContent(t, filepath.Join(root, "nested", "ghw-test-2"), "ghw-test-2\n")
   115  
   116  }
   117  
   118  func verifyFileContent(t *testing.T, path, expected string) {
   119  	data, err := os.ReadFile(path)
   120  	if err != nil {
   121  		t.Fatalf("Expected nil err, but got %v", err)
   122  	}
   123  	content := string(data)
   124  	if content != expected {
   125  		t.Fatalf("Expected %q, but got %q", expected, content)
   126  	}
   127  }