github.com/rrashidov/libpak@v0.0.0-20230911084305-75119185bb4d/sherpa/file_listing_test.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package sherpa_test
    18  
    19  import (
    20  	"crypto/sha256"
    21  	"encoding/hex"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	. "github.com/onsi/gomega"
    27  	"github.com/sclevine/spec"
    28  
    29  	"github.com/paketo-buildpacks/libpak/sherpa"
    30  )
    31  
    32  func testFileListing(t *testing.T, context spec.G, it spec.S) {
    33  	var (
    34  		Expect = NewWithT(t).Expect
    35  
    36  		path string
    37  	)
    38  
    39  	it.Before(func() {
    40  		path = t.TempDir()
    41  	})
    42  
    43  	it("create listing", func() {
    44  		Expect(os.WriteFile(filepath.Join(path, "alpha.txt"), []byte{1}, 0644)).To(Succeed())
    45  		Expect(os.MkdirAll(filepath.Join(path, "test-directory"), 0755)).To(Succeed())
    46  		Expect(os.WriteFile(filepath.Join(path, "test-directory", "bravo.txt"), []byte{2}, 0644)).To(Succeed())
    47  
    48  		e, err := sherpa.NewFileListing(path)
    49  		Expect(err).NotTo(HaveOccurred())
    50  
    51  		Expect(e).To(HaveLen(3))
    52  	})
    53  
    54  	it("create listing skipping .git folder", func() {
    55  		Expect(os.MkdirAll(filepath.Join(path, ".git"), 0755)).To(Succeed())
    56  		Expect(os.WriteFile(filepath.Join(path, ".git", "HEAD"), []byte{1}, 0644)).To(Succeed())
    57  		Expect(os.WriteFile(filepath.Join(path, ".git", "config"), []byte{1}, 0644)).To(Succeed())
    58  		Expect(os.WriteFile(filepath.Join(path, "alpha.txt"), []byte{1}, 0644)).To(Succeed())
    59  		Expect(os.MkdirAll(filepath.Join(path, "test-directory"), 0755)).To(Succeed())
    60  		Expect(os.WriteFile(filepath.Join(path, "test-directory", "bravo.txt"), []byte{2}, 0644)).To(Succeed())
    61  		Expect(os.MkdirAll(filepath.Join(path, "test-directory", ".git"), 0755)).To(Succeed())
    62  		Expect(os.WriteFile(filepath.Join(path, "test-directory", ".git", "config"), []byte{1}, 0644)).To(Succeed())
    63  
    64  		e, err := sherpa.NewFileListing(path)
    65  		Expect(err).NotTo(HaveOccurred())
    66  
    67  		Expect(e).To(HaveLen(3))
    68  	})
    69  
    70  	it("create listing as hash with non-regular file", func() {
    71  		Expect(os.WriteFile(filepath.Join(path, "alpha.txt"), []byte{1}, 0644)).To(Succeed())
    72  		Expect(os.MkdirAll(filepath.Join(path, "test-directory"), 0755)).To(Succeed())
    73  		Expect(os.WriteFile(filepath.Join(path, "test-directory", "bravo.txt"), []byte{2}, 0644)).To(Succeed())
    74  		Expect(os.Symlink(filepath.Join(path, "test-directory"), filepath.Join(path, "symlink-test-dir")))
    75  		Expect(os.Symlink(filepath.Join(path, "test-directory", "bravo.txt"), filepath.Join(path, "symlink-bravo.txt")))
    76  		Expect(os.Symlink("alpha.txt", filepath.Join(path, "symlink-relative.txt")))
    77  
    78  		e, err := sherpa.NewFileListing(path)
    79  		Expect(err).NotTo(HaveOccurred())
    80  
    81  		Expect(e).To(HaveLen(6))
    82  		Expect(e[0].Path).To(HaveSuffix("alpha.txt"))
    83  		Expect(e[1].Path).To(HaveSuffix("symlink-bravo.txt"))
    84  		Expect(e[2].Path).To(HaveSuffix("symlink-relative.txt"))
    85  		Expect(e[3].Path).To(HaveSuffix("symlink-test-dir"))
    86  		Expect(e[4].Path).To(HaveSuffix("test-directory"))
    87  		Expect(e[5].Path).To(HaveSuffix("bravo.txt"))
    88  		Expect(e[1].SHA256).To(Equal(e[5].SHA256)) // symlink to file should have hash of target file
    89  	})
    90  
    91  	it("create listing and get SHA256", func() {
    92  		Expect(os.WriteFile(filepath.Join(path, "alpha.txt"), []byte{}, 0644)).To(Succeed())
    93  		Expect(os.MkdirAll(filepath.Join(path, "test-directory"), 0755)).To(Succeed())
    94  		Expect(os.WriteFile(filepath.Join(path, "test-directory", "bravo.txt"), []byte{}, 0644)).To(Succeed())
    95  
    96  		e, err := sherpa.NewFileListing(path)
    97  		Expect(err).NotTo(HaveOccurred())
    98  
    99  		hash := sha256.New()
   100  		for _, file := range e {
   101  			hash.Write([]byte(file.Path + file.Mode + file.SHA256 + "\n"))
   102  		}
   103  
   104  		s, err := sherpa.NewFileListingHash(path)
   105  		Expect(err).NotTo(HaveOccurred())
   106  
   107  		Expect(s).To(Equal(hex.EncodeToString(hash.Sum(nil))))
   108  	})
   109  }