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

Support windows filepaths / line endings #52

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 4 commits into from
Jul 22, 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
13 changes: 10 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ on:
# TODO - periodically run on a cron to detect API drift.
# schedule:
# - cron: '0 13 * * *'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
build:
name: Build
Expand All @@ -37,16 +42,18 @@ jobs:

test:
name: Matrix Test
needs: build
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
max-parallel: 1
fail-fast: false
matrix:
os:
- "ubuntu-latest"
- "windows-latest"
terraform:
- "1.1.*"
- "1.0.*"
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go
uses: actions/setup-go@v2
Expand All @@ -72,4 +79,4 @@ jobs:
VERCEL_TERRAFORM_TESTING_BITBUCKET_REPO: "dglsparsons-test/test"
VERCEL_TERRAFORM_TESTING_DOMAIN: "dgls.dev"
run: |
go test -v -cover ./...
go test ./...
12 changes: 12 additions & 0 deletions vercel/data_source_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vercel

import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
Expand All @@ -12,6 +13,16 @@ 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 @@ -81,6 +92,7 @@ func (r dataSourceFile) Read(ctx context.Context, req tfsdk.ReadDataSourceReques
)
return
}
content = removeCRLF(content)

rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])
Expand Down
2 changes: 1 addition & 1 deletion vercel/data_source_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAcc_FileDataSource(t *testing.T) {
func TestAcc_DataSourceFile(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
1 change: 1 addition & 0 deletions vercel/data_source_project_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (r dataSourceProjectDirectory) Read(ctx context.Context, req tfsdk.ReadData
)
return
}
content = removeCRLF(content)
rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])

Expand Down
7 changes: 4 additions & 3 deletions vercel/data_source_project_directory_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package vercel_test

import (
"path/filepath"
"testing"

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

func TestAcc_ProjectDirectoryDataSource(t *testing.T) {
func TestAcc_DataSourceProjectDirectory(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -16,8 +17,8 @@ func TestAcc_ProjectDirectoryDataSource(t *testing.T) {
Config: testAccProjectDirectoryConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_project_directory.test", "path", "example"),
resource.TestCheckResourceAttr("data.vercel_project_directory.test", "files.example/index.html", "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68"),
resource.TestCheckNoResourceAttr("data.vercel_project_directory.test", "files.example/file2.html"),
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")),
),
},
},
Expand Down
2 changes: 1 addition & 1 deletion vercel/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (r resourceDeployment) Create(ctx context.Context, req tfsdk.CreateResource
}

err = r.p.client.CreateFile(ctx, client.CreateFileRequest{
Filename: f.File,
Filename: normaliseFilename(f.File, plan.PathPrefix),
SHA: f.Sha,
Content: string(content),
TeamID: plan.TeamID.Value,
Expand Down
41 changes: 30 additions & 11 deletions vercel/resource_deployment_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vercel

import (
"fmt"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -100,6 +101,30 @@ func (p *ProjectSettings) fillNulls() *ProjectSettings {
}
}

/*
* The files uploaded to Vercel need to have some minor adjustments:
* - Legacy behaviour was that any upward navigation ("../") was stripped from the
* start of a file path.
* - Newer behaviour introduced a `path_prefix` that could be specified, that would
* control what part of a relative path to files should be removed prior to uploading
* into Vercel.
* - We want to support this regardless of path separator, the simplest way to do
* this is to ensure all paths are converted to forward slashes, and settings should
* be specified using forward slashes.
* See https://github.com/vercel/terraform-provider-vercel/issues/14#issuecomment-1103973603
* for additional context on the first two points.
*/
func normaliseFilename(filename string, pathPrefix types.String) string {
filename = filepath.ToSlash(filename)
if pathPrefix.Unknown || pathPrefix.Null {
for strings.HasPrefix(filename, "../") {
return strings.TrimPrefix(filename, "../")
}
}

return strings.TrimPrefix(filename, filepath.ToSlash(pathPrefix.Value))
}

// getFiles is a helper for turning the terraform deployment state into a set of client.DeploymentFile
// structs, ready to hit the API with. It also returns a map of files by sha, which is used to quickly
// look up any missing SHAs from the create deployment resposnse.
Expand All @@ -118,25 +143,19 @@ func getFiles(unparsedFiles map[string]string, pathPrefix types.String) ([]clien
}
sha := sizeSha[1]

untrimmedFilename := filename
if pathPrefix.Unknown || pathPrefix.Null {
for strings.HasPrefix(filename, "../") {
filename = strings.TrimPrefix(filename, "../")
}
} else {
filename = strings.TrimPrefix(filename, pathPrefix.Value)
}
file := client.DeploymentFile{
File: filename,
File: normaliseFilename(filename, pathPrefix),
Sha: sha,
Size: size,
}
files = append(files, file)

/* The API can return a set of missing files. When this happens, we want the path name
* complete with the original, untrimmed prefix. */
* complete with the original, untrimmed prefix. This also needs to use the hosts
* path separator. This is so we can read the file.
*/
filesBySha[sha] = client.DeploymentFile{
File: untrimmedFilename,
File: filename,
Sha: sha,
Size: size,
}
Expand Down
1 change: 0 additions & 1 deletion vercel/resource_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func TestAcc_DeploymentWithPathPrefix(t *testing.T) {
}

func TestAcc_DeploymentWithDeleteOnDestroy(t *testing.T) {
t.Parallel()
projectSuffix := acctest.RandString(16)
extraConfig := "delete_on_destroy = true"
deploymentID := ""
Expand Down