这是indexloc提供的服务,不要输入任何密码
Skip to content

Remove windows line-ending conversions #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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