Skip to content

Commit

Permalink
Fixes #852 optionally add last newline
Browse files Browse the repository at this point in the history
Tested with Linux and Windows, by appending many new functions to
a single stack.yml file.

Signed-off-by: Kyle Brennan <[email protected]>
  • Loading branch information
kylos101 committed Dec 13, 2021
1 parent d94600d commit 6436a8f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
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) {
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)
}
}

0 comments on commit 6436a8f

Please sign in to comment.