diff --git a/libimage/download.go b/libimage/download.go index 5ea11f084..54edf1b9a 100644 --- a/libimage/download.go +++ b/libimage/download.go @@ -11,7 +11,7 @@ import ( ) // tmpdir returns a path to a temporary directory. -func (r *Runtime) tmpdir() string { +func tmpdir() string { tmpdir := os.Getenv("TMPDIR") if tmpdir == "" { tmpdir = "/var/tmp" @@ -25,7 +25,7 @@ func (r *Runtime) tmpdir() string { func (r *Runtime) downloadFromURL(source string) (string, error) { fmt.Printf("Downloading from %q\n", source) - outFile, err := ioutil.TempFile(r.tmpdir(), "import") + outFile, err := ioutil.TempFile(r.systemContext.BigFilesTemporaryDir, "import") if err != nil { return "", errors.Wrap(err, "error creating file") } diff --git a/libimage/runtime.go b/libimage/runtime.go index 5e1b6a411..71a4cce1d 100644 --- a/libimage/runtime.go +++ b/libimage/runtime.go @@ -84,6 +84,9 @@ func RuntimeFromStore(store storage.Store, options *RuntimeOptions) (*Runtime, e } else { systemContext = types.SystemContext{} } + if systemContext.BigFilesTemporaryDir == "" { + systemContext.BigFilesTemporaryDir = tmpdir() + } setRegistriesConfPath(&systemContext) diff --git a/libimage/runtime_test.go b/libimage/runtime_test.go index c23aedb13..c33c4721f 100644 --- a/libimage/runtime_test.go +++ b/libimage/runtime_test.go @@ -38,6 +38,7 @@ func testNewRuntime(t *testing.T) (runtime *Runtime, cleanup func()) { runtime, err = RuntimeFromStoreOptions(&RuntimeOptions{SystemContext: systemContext}, storeOptions) require.NoError(t, err) + require.Equal(t, runtime.systemContext.BigFilesTemporaryDir, tmpdir()) cleanup = func() { runtime.Shutdown(true) diff --git a/pkg/report/template.go b/pkg/report/template.go index f7b4506bb..f86b07034 100644 --- a/pkg/report/template.go +++ b/pkg/report/template.go @@ -130,7 +130,7 @@ func NewTemplate(name string) *Template { func (t *Template) Parse(text string) (*Template, error) { if strings.HasPrefix(text, "table ") { t.isTable = true - text = "{{range .}}" + NormalizeFormat(text) + "{{end}}" + text = "{{range .}}" + NormalizeFormat(text) + "{{end -}}" } else { text = NormalizeFormat(text) } @@ -157,12 +157,12 @@ func (t *Template) IsTable() bool { return t.isTable } -var rangeRegex = regexp.MustCompile(`{{\s*range\s*\.\s*}}.*{{\s*end\s*}}`) +var rangeRegex = regexp.MustCompile(`{{\s*range\s*\.\s*}}.*{{\s*end\s*-?\s*}}`) // EnforceRange ensures that the format string contains a range func EnforceRange(format string) string { if !rangeRegex.MatchString(format) { - return "{{range .}}" + format + "{{end}}" + return "{{range .}}" + format + "{{end -}}" } return format } diff --git a/pkg/report/template_test.go b/pkg/report/template_test.go index cdf84c9a8..db75a6fab 100644 --- a/pkg/report/template_test.go +++ b/pkg/report/template_test.go @@ -158,8 +158,43 @@ func TestTemplate_HasTable(t *testing.T) { } func TestTemplate_EnforceRange(t *testing.T) { - testRange := `{{range .}}foobar was here{{end}}` + testRange := `{{range .}}foobar was here{{end -}}` assert.Equal(t, testRange, EnforceRange(testRange)) assert.Equal(t, testRange, EnforceRange("foobar was here")) assert.NotEqual(t, testRange, EnforceRange("foobar")) + + // Do not override a given range + testRange = `{{range .}}foobar was here{{end}}` + assert.Equal(t, testRange, EnforceRange(testRange)) +} + +func TestTemplate_Newlines(t *testing.T) { + input := []struct { + Field1 string + Field2 int + Field3 string + }{ + {Field1: "One", Field2: 1, Field3: "First"}, + {Field1: "Two", Field2: 2, Field3: "Second"}, + {Field1: "Three", Field2: 3, Field3: "Third"}, + } + + hdrs := Headers(input[0], map[string]string{"Field1": "Ein", "Field2": "Zwei", "Field3": "Drei"}) + + // Ensure no blank lines in table + expected := "EIN\tZWEI\tDREI\nOne\t1\tFirst\nTwo\t2\tSecond\nThree\t3\tThird\n" + + format := NormalizeFormat("{{.Field1}}\t{{.Field2}}\t{{.Field3}}") + format = EnforceRange(format) + tmpl, err := NewTemplate("TestTemplate").Parse(format) + assert.NoError(t, err) + + var buf bytes.Buffer + err = tmpl.Execute(&buf, hdrs) + assert.NoError(t, err) + + err = tmpl.Execute(&buf, input) + assert.NoError(t, err) + + assert.Equal(t, expected, buf.String()) }