Skip to content

Commit

Permalink
Sanitise name when packing the wasm file (#401)
Browse files Browse the repository at this point in the history
* Sanitise name when packing the wasm file

This matches what build does and fixes deploy when name includes
e.g., whitespace.

* Fix test with recent rust versions
  • Loading branch information
fgsch authored Sep 13, 2021
1 parent 1281c72 commit 47eec1d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/compute/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestBuildRust(t *testing.T) {
client: versionClient{
fastlyVersions: []string{"0.6.0"},
},
wantError: "rustc constraint '>= 1.0.0 < 1.40.0' not met: 1.54.0",
wantError: "rustc constraint '>= 1.0.0 < 1.40.0' not met:",
wantRemediationError: "Run `rustup update stable`, or ensure your `rust-toolchain` file specifies a version matching the constraint (e.g. `channel = \"stable\"`).",
},
{
Expand Down
8 changes: 5 additions & 3 deletions pkg/commands/compute/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/filesystem"
"github.com/fastly/cli/pkg/text"
"github.com/kennygrant/sanitize"
"github.com/mholt/archiver/v3"
)

Expand Down Expand Up @@ -51,7 +52,8 @@ func (c *PackCommand) Exec(in io.Reader, out io.Writer) (err error) {
}
}(c.Globals.ErrLog)

pkg := fmt.Sprintf("pkg/%s/bin/main.wasm", c.manifest.File.Name)
name := sanitize.BaseName(c.manifest.File.Name)
pkg := fmt.Sprintf("pkg/%s/bin/main.wasm", name)
dir := filepath.Dir(pkg)
err = filesystem.MakeDirectoryIfNotExists(dir)
if err != nil {
Expand Down Expand Up @@ -93,7 +95,7 @@ func (c *PackCommand) Exec(in io.Reader, out io.Writer) (err error) {

progress.Step("Copying manifest...")
src = manifest.Filename
dst = fmt.Sprintf("pkg/%s/%s", c.manifest.File.Name, manifest.Filename)
dst = fmt.Sprintf("pkg/%s/%s", name, manifest.Filename)
if err := filesystem.CopyFile(src, dst); err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]interface{}{
"Manifest (destination)": dst,
Expand All @@ -106,7 +108,7 @@ func (c *PackCommand) Exec(in io.Reader, out io.Writer) (err error) {
tar := archiver.NewTarGz()
tar.OverwriteExisting = true
{
dir := fmt.Sprintf("pkg/%s", c.manifest.File.Name)
dir := fmt.Sprintf("pkg/%s", name)
src := []string{dir}
dst := fmt.Sprintf("%s.tar.gz", dir)
if err = tar.Archive(src, dst); err != nil {
Expand Down
20 changes: 19 additions & 1 deletion pkg/commands/compute/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestPack(t *testing.T) {
wantOutput []string
expectedFiles [][]string
}{
// The following test validates that the expected directory struture was
// The following test validates that the expected directory structure was
// created successfully.
{
name: "success",
Expand All @@ -39,6 +39,24 @@ func TestPack(t *testing.T) {
{"pkg", "precompiled.tar.gz"},
},
},
// The following test validates that the expected directory structure was
// created successfully when `name` contains whitespace.
{
name: "success",
args: args("compute pack --path ./main.wasm"),
manifest: `name = "another name"`,
wantOutput: []string{
"Initializing...",
"Copying wasm binary...",
"Copying manifest...",
"Creating .tar.gz file...",
},
expectedFiles: [][]string{
{"pkg", "another-name", "bin", "main.wasm"},
{"pkg", "another-name", "fastly.toml"},
{"pkg", "another-name.tar.gz"},
},
},
// The following tests validate that a valid path flag value should be
// provided.
{
Expand Down

0 comments on commit 47eec1d

Please sign in to comment.