Skip to content

Commit

Permalink
Normalize relative paths to absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Aug 8, 2023
1 parent 6a7ca7a commit d73a664
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 4 deletions.
20 changes: 20 additions & 0 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ func (p *Packer) Pack(src string, w io.Writer) (*Meta, error) {
ignoreRules = parseIgnoreFile(src)
}

if !filepath.IsAbs(src) {
src, err = filepath.Abs(src)
if err != nil {
return nil, fmt.Errorf("something went super wrong root")
}
}

// Walk the tree of files.
err = filepath.Walk(src, p.packWalkFn(src, src, src, tarW, meta, ignoreRules))
if err != nil {
Expand All @@ -183,6 +190,19 @@ func (p *Packer) packWalkFn(root, src, dst string, tarW *tar.Writer, meta *Meta,
return err
}

if !filepath.IsAbs(src) {
src, err = filepath.Abs(src)
if err != nil {
return fmt.Errorf("failed to get absolute path for src %q: %w", src, err)
}
}

if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to get absolute path for file %q: %w", path, err)
}
}
// Get the relative path from the current src directory.
subpath, err := filepath.Rel(src, path)
if err != nil {
Expand Down
42 changes: 38 additions & 4 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ func TestPack_rootIsSymlink(t *testing.T) {
}
}

func TestPack_absoluteSrcRelativeSymlinks(t *testing.T) {
var path string

// In instances we run within CI, we want to fetch
// the absolute path where our test is located
if workDir, ok := os.LookupEnv("GITHUB_WORKSPACE"); ok {
path = workDir
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Could not read home dir: %v", err)
}
// !! Ugly code alert: This tests assumes the repository
// lives in $HOME/hashicorp/go-slug. You may need to change
// this according to your needs
path = filepath.Join(homeDir, "hashicorp/go-slug")
}

// One last check, if this variable is empty we'll error
// since we need the absolute path for the source
if path == "" {
t.Fatal("Home directory could not be determined")
}

path = filepath.Join(path, "testdata/archive-dir-absolute/dev")
slug := bytes.NewBuffer(nil)
_, err := Pack(path, slug, true)
if err != nil {
// We simply want to ensure paths can be resolved while
// traversing the source directory
t.Fatalf("err: %v", err)
}
}

func TestPackWithoutIgnoring(t *testing.T) {
slug := bytes.NewBuffer(nil)

Expand Down Expand Up @@ -1088,8 +1122,8 @@ func assertArchiveFixture(t *testing.T, slug *bytes.Buffer, got *Meta) {
}

if hdr.Name == "example.tf" {
if hdr.Typeflag != tar.TypeSymlink {
t.Fatalf("expected symlink file 'example.tf'")
if hdr.Typeflag != tar.TypeReg {
t.Fatalf("expected symlink to be dereferenced 'example.tf'")
}
externalTargetFound = true
}
Expand All @@ -1100,9 +1134,9 @@ func assertArchiveFixture(t *testing.T, slug *bytes.Buffer, got *Meta) {
t.Fatal("expected to find symlink")
}

// Make sure we saw and handled a nested symlink
// Make sure we saw and handled a dereferenced symlink
if !externalTargetFound {
t.Fatal("expected to find nested symlink")
t.Fatal("expected to find dereferenced symlink")
}

// Make sure the .git directory is ignored
Expand Down
2 changes: 2 additions & 0 deletions testdata/archive-dir-absolute/_common/extra-files/bar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo "bar"
2 changes: 2 additions & 0 deletions testdata/archive-dir-absolute/_common/extra-files/foo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo "foo"
3 changes: 3 additions & 0 deletions testdata/archive-dir-absolute/_common/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
app = "service-01"
}
7 changes: 7 additions & 0 deletions testdata/archive-dir-absolute/_common/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
files = fileset("${path.module}/extra-files", "*.sh")
}

output "scripts" {
value = local.files
}
3 changes: 3 additions & 0 deletions testdata/archive-dir-absolute/_common/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "~> 1.2"
}
10 changes: 10 additions & 0 deletions testdata/archive-dir-absolute/dev/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
remote "backend" {
hostname = "foobar.terraform.io"
organization = "hashicorp"

workspaces {
name = "dev-service-02"
}
}
}
1 change: 1 addition & 0 deletions testdata/archive-dir-absolute/dev/extra-files
1 change: 1 addition & 0 deletions testdata/archive-dir-absolute/dev/locals.tf
1 change: 1 addition & 0 deletions testdata/archive-dir-absolute/dev/output.tf
4 changes: 4 additions & 0 deletions testdata/archive-dir-absolute/dev/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
locals {
env = "dev"
region = "us-east-2"
}
1 change: 1 addition & 0 deletions testdata/archive-dir-absolute/dev/versions.tf

0 comments on commit d73a664

Please sign in to comment.