-
Notifications
You must be signed in to change notification settings - Fork 61
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
Changes from 2 commits
669872d
dd5db5c
bfc4e59
0bdf3b6
db02a44
cffc8e4
76ac3f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ☝️ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
|
@@ -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) | ||
} | ||
}) | ||
} | ||
} | ||
|
@@ -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() | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] |
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello world!") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬