Skip to content

Commit

Permalink
Remove windows line-ending conversions (#63)
Browse files Browse the repository at this point in the history
* Remove windows line-ending conversions

This was originally implemented to prevent spurious diffs when
swapping the same repository between windows and unix systems.

However, due to the lack of detection of binary files, such as png
images, this can cause some files to be corrupted.

Looking at the [file command
behaviour](https://github.com/file/file/blob/f2a6e7cb7db9b5fd86100403df6b2f830c7f22ba/src/encoding.c#L151-L228)
it seems like detecting binary/non-binary files is extremely
non-trivial, and heuristic at best (meaning it could be wrong).

In order to prevent hard to detect/analyse issues, I think it's best to
just accept that some spurious changes can occur if swapping between
systems.

* Fix tests to work on windows
  • Loading branch information
dglsparsons authored Aug 26, 2022
1 parent d14148b commit f4ffc2a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
12 changes: 0 additions & 12 deletions vercel/data_source_file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vercel

import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
Expand All @@ -15,16 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

/*
* To have the content hash and file sizes remain consistent between different operating systems
* (important as this prevents spurious file-changes, and thus spurious file uploads), make sure
* the file content handles line-endings consistently, regardless of the host platform.
* The simplest way to achieve this is just to replace CRLF with LF.
*/
func removeCRLF(content []byte) []byte {
return bytes.ReplaceAll(content, []byte("\r\n"), []byte("\n"))
}

type dataSourceFileType struct{}

// GetSchema returns the schema information for a file data source
Expand Down Expand Up @@ -94,7 +83,6 @@ func (r dataSourceFile) Read(ctx context.Context, req datasource.ReadRequest, re
)
return
}
content = removeCRLF(content)

rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])
Expand Down
37 changes: 36 additions & 1 deletion vercel/data_source_file_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
package vercel_test

import (
"fmt"
"runtime"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

type Checksums struct {
windows string
unix string
}

func testChecksum(n, attribute string, checksums Checksums) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("not found: %s", n)
}

sizeAndChecksum := rs.Primary.Attributes[attribute]
if runtime.GOOS == "windows" {
if sizeAndChecksum != checksums.windows {
return fmt.Errorf("attribute %s expected %s but got %s", attribute, checksums.unix, sizeAndChecksum)
}

return nil
}

if sizeAndChecksum != checksums.unix {
return fmt.Errorf("attribute %s expected %s but got %s", attribute, checksums.unix, sizeAndChecksum)
}

return nil
}
}

func TestAcc_DataSourceFile(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Expand All @@ -17,7 +49,10 @@ func TestAcc_DataSourceFile(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_file.test", "path", "example/index.html"),
resource.TestCheckResourceAttr("data.vercel_file.test", "id", "example/index.html"),
resource.TestCheckResourceAttr("data.vercel_file.test", "file.example/index.html", "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68"),
testChecksum("data.vercel_file.test", "file.example/index.html", Checksums{
unix: "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68",
windows: "65~c0b8b91602dc7a394354cd9a21460ce2070b9a13",
}),
),
},
},
Expand Down
1 change: 0 additions & 1 deletion vercel/data_source_project_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (r dataSourceProjectDirectory) Read(ctx context.Context, req datasource.Rea
)
return
}
content = removeCRLF(content)
rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])

Expand Down
10 changes: 8 additions & 2 deletions vercel/data_source_project_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ func TestAcc_DataSourceProjectDirectory(t *testing.T) {
Config: testAccProjectDirectoryConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_project_directory.test", "path", "example"),
resource.TestCheckResourceAttr("data.vercel_project_directory.test", filepath.Join("files.example", "index.html"), "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68"),
resource.TestCheckNoResourceAttr("data.vercel_project_directory.test", filepath.Join("files.example", "file2.html")),
testChecksum("data.vercel_project_directory.test", filepath.Join("files.example", "index.html"), Checksums{
unix: "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68",
windows: "65~c0b8b91602dc7a394354cd9a21460ce2070b9a13",
}),
resource.TestCheckNoResourceAttr(
"data.vercel_project_directory.test",
filepath.Join("files.example", "file2.html"),
),
),
},
},
Expand Down
1 change: 0 additions & 1 deletion vercel/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ func (r resourceDeployment) Create(ctx context.Context, req resource.CreateReque
)
return
}
content = removeCRLF(content)

err = r.p.client.CreateFile(ctx, client.CreateFileRequest{
Filename: normaliseFilename(f.File, plan.PathPrefix),
Expand Down

0 comments on commit f4ffc2a

Please sign in to comment.