From 31037f0a677d8ae838f39b1d59c4e0050da84683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Paix=C3=A3o?= Date: Fri, 20 Oct 2017 19:54:28 -0200 Subject: [PATCH] fix bug https://github.com/tsuru/tsuru/issues/1764 --- tasks.go | 10 +++++----- tasks_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/tasks.go b/tasks.go index 015201e..5a2a3c9 100644 --- a/tasks.go +++ b/tasks.go @@ -5,6 +5,7 @@ package main import ( + "bytes" "fmt" "io" "io/ioutil" @@ -125,9 +126,8 @@ func buildHooks(yamlData TsuruYaml, envs []bind.EnvVar) error { return execScript(cmds, envs, os.Stdout) } -func readProcfile() (string, error) { - procfilePath := fmt.Sprintf("%s/%s", defaultWorkingDir, "Procfile") - f, err := filesystem().Open(procfilePath) +func readProcfile(path string) (string, error) { + f, err := filesystem().Open(fmt.Sprintf("%v/Procfile", path)) if err != nil { return "", err } @@ -136,13 +136,13 @@ func readProcfile() (string, error) { if err != nil { return "", err } - return string(procfile), nil + return string(bytes.Replace(procfile, []byte("\r\n"), []byte("\n"), -1)), nil } var procfileRegex = regexp.MustCompile(`^([\w-]+):\s*(\S.+)$`) func loadProcesses(t *TsuruYaml) error { - procfile, err := readProcfile() + procfile, err := readProcfile(defaultWorkingDir) if err != nil { return err } diff --git a/tasks_test.go b/tasks_test.go index 05d89aa..4ac71fe 100644 --- a/tasks_test.go +++ b/tasks_test.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "os" + "syscall" "github.com/tsuru/tsuru/app/bind" "github.com/tsuru/tsuru/exec/exectest" @@ -217,3 +218,37 @@ func (s *S) TestDiffDeploy(c *check.C) { c.Assert(result, check.DeepEquals, diff) c.Assert(first, check.Equals, false) } + +func (s *S) TestReadProcfileNotFound(c *check.C) { + _, err := readProcfile("./fake-path") + _, ok := err.(syscall.Errno) + c.Assert(ok, check.Equals, true) +} + +func (s *S) TestReadProcfileFound(c *check.C) { + expected := "web: ls\naxl: \"echo Guns N' Roses\"" + procfilePath := "." + procfileContent := expected + procfile, err := s.fs.Create(fmt.Sprintf("%v/Procfile", procfilePath)) + c.Assert(err, check.IsNil) + _, err = procfile.Write([]byte(procfileContent)) + c.Assert(err, check.IsNil) + c.Assert(procfile.Close(), check.IsNil) + result, err := readProcfile(procfilePath) + c.Assert(err, check.IsNil) + c.Assert(result, check.Equals, expected) +} + +func (s *S) TestReadProcfileNormalizeCRLFToLF(c *check.C) { + procfilePath := "." + procfileContent := "web: ls\r\nslash: \"echo Guns N' Roses\"" + expected := "web: ls\nslash: \"echo Guns N' Roses\"" + procfile, err := s.fs.Create(fmt.Sprintf("%v/Procfile", procfilePath)) + c.Assert(err, check.IsNil) + _, err = procfile.Write([]byte(procfileContent)) + c.Assert(err, check.IsNil) + c.Assert(procfile.Close(), check.IsNil) + result, err := readProcfile(procfilePath) + c.Assert(err, check.IsNil) + c.Assert(result, check.Equals, expected) +}