Skip to content
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

Filter unwanted files from template repository whilst initialising #1

Merged
merged 7 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ fastly

# Fastly package format files
**/fastly.toml
!pkg/packag/testdata/build/fastly.toml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬

!pkg/compute/testdata/build/fastly.toml
**/Cargo.toml
!pkg/packag/testdata/build/Cargo.toml
!pkg/compute/testdata/build/Cargo.toml
**/Cargo.lock
!pkg/packag/testdata/build/Cargo.lock
!pkg/compute/testdata/build/Cargo.lock
**/*.tar.gz
!pkg/packag/testdata/deploy/pkg/package.tar.gz
!pkg/compute/testdata/deploy/pkg/package.tar.gz
**/bin
**/src
!pkg/packag/testdata/build/src
!pkg/compute/testdata/build/src
**/target

# Binaries for programs and plugins
Expand All @@ -33,4 +33,4 @@ fastly

# Ignore binaries
build/
!pkg/packag/testdata/build/
!pkg/compute/testdata/build/
124 changes: 113 additions & 11 deletions pkg/compute/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,81 @@ func TestInit(t *testing.T) {
t.Skip("Set TEST_COMPUTE_INIT to run this test")
}

if err := os.Chdir("testdata/init"); err != nil {
t.Fatal(err)
}

for _, testcase := range []struct {
name string
args []string
wantError string
wantOutput string
name string
args []string
wantFiles []string
unwantedFiles []string
wantError string
wantOutput []string
manifestContent string
phamann marked this conversation as resolved.
Show resolved Hide resolved
}{
// TODO(pb): wait until package template repo is public
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly confused, the repo is now public, hence why I've written these tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, I was just having a little laugh, please ignore :)

{
name: "unkown repository",
args: []string{"compute", "init", "--from", "https://example.com/template"},
wantError: "error fetching package template: repository not found",
},
{
name: "with name",
args: []string{"compute", "init", "--name", "test"},
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest..",
},
manifestContent: `name = "test"`,
},
{
name: "with service",
args: []string{"compute", "init", "--service-id", "test"},
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest..",
},
manifestContent: `service_id = "test"`,
},
{
name: "default",
args: []string{"compute", "init"},
wantFiles: []string{
"cargo.toml",
"fastly.toml",
"src/main.rs",
},
unwantedFiles: []string{
"SECURITY.md",
},
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest..",
},
},
} {
t.Run(testcase.name, func(t *testing.T) {
// We're going to chdir to an init environment,
// so save the PWD to return to, afterwards.
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

// Create our init environment in a temp dir.
// Defer a call to clean it up.
rootdir := makeInitEnvironment(t)
defer os.RemoveAll(rootdir)

fmt.Printf("TEMP DIRECTORY %s", rootdir)
phamann marked this conversation as resolved.
Show resolved Hide resolved

// Before running the test, chdir into the init environment.
// When we're done, chdir back to our original location.
// This is so we can reliably assert file structure.
if err := os.Chdir(rootdir); err != nil {
t.Fatal(err)
}
defer os.Chdir(pwd)

var (
args = testcase.args
env = config.Environment{}
Expand All @@ -55,9 +117,28 @@ func TestInit(t *testing.T) {
buf bytes.Buffer
out io.Writer = common.NewSyncWriter(&buf)
)
err := app.Run(args, env, file, appConfigFile, clientFactory, httpClient, versioner, in, out)
err = app.Run(args, env, file, appConfigFile, clientFactory, httpClient, versioner, in, out)
testutil.AssertErrorContains(t, err, testcase.wantError)
testutil.AssertString(t, testcase.wantOutput, buf.String())
for _, file := range testcase.wantFiles {
if _, err := os.Stat(filepath.Join(rootdir, file)); err != nil {
t.Errorf("wanted file %s not found", file)
}
}
for _, file := range testcase.unwantedFiles {
if _, err := os.Stat(filepath.Join(rootdir, file)); err == nil {
phamann marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("found unwanted file %s found", file)
phamann marked this conversation as resolved.
Show resolved Hide resolved
}
}
for _, s := range testcase.wantOutput {
testutil.AssertStringContains(t, buf.String(), s)
}
if testcase.manifestContent != "" {
content, err := ioutil.ReadFile(filepath.Join(rootdir, compute.ManifestFilename))
if err != nil {
t.Fatal(err)
}
testutil.AssertStringContains(t, string(content), testcase.manifestContent)
}
})
}
}
Expand Down Expand Up @@ -626,6 +707,27 @@ func TestUploadPackage(t *testing.T) {
}
}

func makeInitEnvironment(t *testing.T) (rootdir string) {
t.Helper()

p := make([]byte, 8)
n, err := rand.Read(p)
if err != nil {
t.Fatal(err)
}

rootdir = filepath.Join(
os.TempDir(),
fmt.Sprintf("fastly-build-%x", p[:n]),
)

if err := os.MkdirAll(rootdir, 0700); err != nil {
t.Fatal(err)
}

return rootdir
}

func makeBuildEnvironment(t *testing.T, manifestContent string) (rootdir string) {
t.Helper()

Expand Down
14 changes: 13 additions & 1 deletion pkg/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"time"

"github.com/fastly/cli/pkg/common"
Expand All @@ -15,6 +16,13 @@ import (
"gopkg.in/src-d/go-git.v4"
)

const defaultTemplate = "https://github.com/fastly/fastly-template-rust-default"

var (
fastlyOrgRegEx = regexp.MustCompile(`github\.com\/fastly`)
phamann marked this conversation as resolved.
Show resolved Hide resolved
fastlyFileIgnoreListRegEx = regexp.MustCompile(`\.github|LICENSE|SECURITY\.md`)
)

// InitCommand initializes a Compute@Edge project package on the local machine.
type InitCommand struct {
common.Base
Expand All @@ -30,7 +38,7 @@ func NewInitCommand(parent common.Registerer, globals *config.Data) *InitCommand
c.Globals = globals
c.CmdClause = parent.Command("init", "Initialize a new Compute@Edge package locally")
c.CmdClause.Flag("name", "Name of package, defaulting to directory name of the --path destination").Short('n').StringVar(&c.name)
c.CmdClause.Flag("from", "Git repository containing package template").Short('f').Default("https://github.com/fastly/fastly-template-rust-default").StringVar(&c.from)
c.CmdClause.Flag("from", "Git repository containing package template").Short('f').Default(defaultTemplate).StringVar(&c.from)
c.CmdClause.Flag("path", "Destination to write the new package, defaulting to the current directory").Short('p').StringVar(&c.path)
c.CmdClause.Flag("service-id", "Optional Fastly service ID written to the package manifest, where this package will be deployed").Short('s').StringVar(&c.serviceID)
return &c
Expand Down Expand Up @@ -103,6 +111,10 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
if err != nil {
return err
}
// Filter any files we want to ignore in Fastly-owned templates.
if fastlyOrgRegEx.MatchString(c.from) && fastlyFileIgnoreListRegEx.MatchString(rel) {
return nil
}
dst := filepath.Join(c.path, rel)
if err := os.MkdirAll(filepath.Dir(dst), 0750); err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions pkg/compute/testdata/build/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkg/compute/testdata/build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "edge-compute-default-rust-template"
version = "0.1.0"
authors = ["phamann <patrick@fastly.com>"]
edition = "2018"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the correct edition. See here for more info: https://doc.rust-lang.org/edition-guide/editions/index.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's the Rust edition, I see 👍


[profile.release]
debug = true

[dependencies]
5 changes: 5 additions & 0 deletions pkg/compute/testdata/build/fastly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
manifest_version = "0.1.0"
name = "Default Rust template"
description = "Default package template for Rust based edge compute projects."
authors = ["phamann <patrick@fastly.com>"]
language = "rust"
3 changes: 3 additions & 0 deletions pkg/compute/testdata/build/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello world!")
}
Binary file added pkg/compute/testdata/deploy/pkg/package.tar.gz
Binary file not shown.