Skip to content

Commit

Permalink
Add CopyFileFrom
Browse files Browse the repository at this point in the history
Resolves #354.

Adds CopyFileFrom convenience method that opens the source file and delegates to CopyFile.

Signed-off-by: Daniel Mikusa <[email protected]>
  • Loading branch information
dmikusa committed Oct 10, 2024
1 parent 92124be commit 8811713
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
14 changes: 13 additions & 1 deletion sherpa/copy_file.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,3 +48,15 @@ func CopyFile(source *os.File, destination string) error {

return nil
}

// CopyFileFrom copies a file from the source to the destination. It ensures that the parent directory is created and
// matches the source and destination permissions.
func CopyFileFrom(source string, destination string) error {
in, err := os.Open(source)
if err != nil {
return fmt.Errorf("unable to open %s\n%w", source, err)
}
defer in.Close()

return CopyFile(in, destination)
}
55 changes: 41 additions & 14 deletions sherpa/copy_file_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,9 @@
package sherpa_test

import (
"fmt"
"os"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -30,35 +32,60 @@ func testCopyFile(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

source *os.File
destination string
rootPath string
sourcePath string
)

expectFilePermissionsToMatch := func(destination string, fileMode os.FileMode) {
fileInfo, err := os.Stat(destination)
fmt.Println("eperms:", fileInfo.Mode().Perm().String())
Expect(err).NotTo(HaveOccurred())
Expect(fileInfo.Mode().Perm().String()).To(Equal(fileMode.String()))
}

it.Before(func() {
var err error

source, err = os.CreateTemp("", "copy-file")
rootPath = t.TempDir()

source, err := os.CreateTemp(rootPath, "source.txt")
Expect(err).NotTo(HaveOccurred())
sourcePath = source.Name()

_, err = source.WriteString("test")
Expect(err).NotTo(HaveOccurred())
Expect(source.Close()).To(Succeed())
source, err = os.Open(source.Name())
Expect(err).NotTo(HaveOccurred())

f, err := os.CreateTemp("", "copy-file")
Expect(err).NotTo(HaveOccurred())
Expect(f.Close()).To(Succeed())
destination = f.Name()
})

it.After(func() {
Expect(os.RemoveAll(source.Name())).To(Succeed())
Expect(os.RemoveAll(destination)).To(Succeed())
Expect(os.RemoveAll(rootPath)).To(Succeed())
})

it("create listing", func() {
it("copy file with permissions", func() {
destination := filepath.Join(rootPath, "destination/nested/destination.txt")

source, err := os.Open(sourcePath)
Expect(err).NotTo(HaveOccurred())
defer source.Close()

expectedMode := os.FileMode(0640)
Expect(os.Chmod(sourcePath, expectedMode)).To(Succeed())

Expect(sherpa.CopyFile(source, destination)).To(Succeed())
Expect(os.ReadFile(destination)).To(Equal([]byte("test")))

expectFilePermissionsToMatch(destination, expectedMode)
})

it.Focus("copy file from string path with permissions", func() {
destination := filepath.Join(rootPath, "destination/other-nested/destination.txt")

expectedMode := os.FileMode(0440)
Expect(os.Chmod(sourcePath, expectedMode)).To(Succeed())

Expect(sherpa.CopyFileFrom(sourcePath, destination)).To(Succeed())
Expect(os.ReadFile(destination)).To(Equal([]byte("test")))

expectFilePermissionsToMatch(destination, expectedMode)
})
}

0 comments on commit 8811713

Please sign in to comment.