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

Fixes #852 optionally add last newline #902

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ Download templates:
}

fileName = appendFile

if err := addEofNewlines("./" + fileName); err != nil {
return err
}

outputMsg = fmt.Sprintf("Stack file updated: %s\n", fileName)

} else {
Expand Down Expand Up @@ -346,3 +351,28 @@ Cannot have duplicate function names in same yaml file`, functionName, appendFil

return nil
}

func addEofNewlines(fileName string) error {
bytes, err := os.ReadFile(fileName)
if err != nil {
return fmt.Errorf("could not open '%s' to check for new lines %s", fileName, err)
}

content := string(bytes)
hasLastNewline := strings.HasSuffix(content, "\n")
if hasLastNewline {
return nil
}

file, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("could not open '%s' to append new lines %s", fileName, err)
}
defer file.Close()

if _, err = file.WriteString("\n\n"); err != nil {
return fmt.Errorf("could not write to '%s' to append new lines %s", fileName, err)
}

return nil
}
70 changes: 70 additions & 0 deletions commands/new_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ func tearDownNewFunction(t *testing.T, functionName string) {
t.Log(err)
}
}
if _, err := os.Stat("stack.yml"); err == nil {
if err := os.Remove("stack.yml"); err != nil {
t.Log(err)
}
}
hDir := handlerDir
if len(hDir) == 0 {
hDir = functionName
Expand Down Expand Up @@ -423,3 +428,68 @@ func Test_getPrefixValue_Flag(t *testing.T) {
t.Errorf("want %s, got %s", want, val)
}
}

func Test_addEofNewlines_works(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Where is this getting tested?

I'd expect something like if wantedYAML == os.ReadFile("stack.yml")

Copy link
Author

Choose a reason for hiding this comment

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

Nice catch, I just added this. Let me know if anything else is needed?

table := []struct {
funcName string
}{
// TODO: after #906 is fixed
// -f or --yaml will work
// and we can change stack to func1
{"stack"},
{"func2"},
{"func3"},
}
resetForTest()
templatePullLocalTemplateRepo(t)

defer tearDownFetchTemplates(t)
const stackFile = "stack.yml"

for i, row := range table {
const functionLang = "ruby"

parameters := []string{
"new",
row.funcName,
"--lang=" + functionLang,
}

if i == 0 {
// there's a bug where this doesn't get honored
// as a workaround, func1 is "stack" in the test table
// this way we predictability create stack.yml
parameters = append(parameters, "--yaml="+stackFile)
} else {
parameters = append(parameters, "--append="+stackFile)
}

faasCmd.SetArgs(nil)
faasCmd.SetArgs(parameters)
faasCmd.Execute()

// drop last two bytes (\n\n) for the first function
if i == 0 {
func(fileName string) {
file, _ := os.OpenFile(fileName, os.O_RDWR, 0600)
defer file.Close()
fileStats, _ := file.Stat()
file.Truncate(fileStats.Size() - 2)
}(stackFile)
} else {
func(fileName string) {
bytes, _ := os.ReadFile(fileName)
want := "\n\n"
got := string(bytes)
matches := strings.HasSuffix(got, want)
if !matches {
t.Fatalf("%q must end with: %q\n", fileName, want)
}
}(stackFile)
}
}

for _, row := range table {
defer tearDownNewFunction(t, row.funcName)
}
}