github.com/paketoio/libpak@v1.3.1/internal/formatter_test.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package internal_test 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/buildpacks/libcnb" 24 "github.com/heroku/color" 25 . "github.com/onsi/gomega" 26 "github.com/paketoio/libpak/internal" 27 "github.com/sclevine/spec" 28 ) 29 30 func testFormatter(t *testing.T, context spec.G, it spec.S) { 31 var ( 32 Expect = NewWithT(t).Expect 33 ) 34 35 context("EnvironmentFormatter", func() { 36 it("formats contents", func() { 37 env := map[string]string{ 38 "test-key-1": "test-value-1", 39 "test-key-2": "test-value-2", 40 } 41 42 Expect(internal.EnvironmentFormatter{Path: "test-path", Environment: env}.String()). 43 To(Equal("Writing test-path/test-key-1\nWriting test-path/test-key-2")) 44 }) 45 }) 46 47 context("LaunchFormatter", func() { 48 49 it("is empty without slices or processes", func() { 50 launch := libcnb.Launch{} 51 52 Expect(internal.LaunchFormatter(launch).String()). 53 To(Equal("")) 54 }) 55 56 it("formats contents with slices", func() { 57 launch := libcnb.Launch{ 58 Slices: []libcnb.Slice{{}, {}}, 59 } 60 61 Expect(internal.LaunchFormatter(launch).String()). 62 To(Equal("2 application slices")) 63 }) 64 65 it("formats contents with processes", func() { 66 launch := libcnb.Launch{ 67 Processes: []libcnb.Process{ 68 { 69 Type: "test-type", 70 Command: "test-command", 71 }, 72 }, 73 } 74 75 Expect(internal.LaunchFormatter(launch).String()). 76 To(Equal("Process types:\n \x1b[36mtest-type\x1b[0m: test-command")) 77 }) 78 79 it("formats contents with slices and processes", func() { 80 launch := libcnb.Launch{ 81 Slices: []libcnb.Slice{{}, {}}, 82 Processes: []libcnb.Process{ 83 { 84 Type: "test-type", 85 Command: "test-command", 86 }, 87 }, 88 } 89 90 Expect(internal.LaunchFormatter(launch).String()). 91 To(Equal("2 application slices\nProcess types:\n \x1b[36mtest-type\x1b[0m: test-command")) 92 }) 93 }) 94 95 context("ProcessesFormatter", func() { 96 97 it("aligns process types", func() { 98 Expect(internal.ProcessesFormatter([]libcnb.Process{ 99 {Type: "short", Command: "test-command-1"}, 100 {Type: "a-very-long-type", Command: "test-command-2"}, 101 }).String()).To(Equal( 102 fmt.Sprintf(" %s: test-command-2\n %s: test-command-1", 103 color.CyanString("a-very-long-type"), color.CyanString("short")))) 104 }) 105 106 it("appends arguments", func() { 107 Expect(internal.ProcessesFormatter([]libcnb.Process{ 108 {Type: "test-type", Command: "test-command", Arguments: []string{"test-arg-1", "test-arg-2"}}, 109 }).String()).To(Equal( 110 fmt.Sprintf(" %s: test-command test-arg-1 test-arg-2", color.CyanString("test-type")))) 111 }) 112 113 it("indicates direct", func() { 114 Expect(internal.ProcessesFormatter([]libcnb.Process{ 115 {Type: "test-type", Command: "test-command", Direct: true}, 116 }).String()).To(Equal( 117 fmt.Sprintf(" %s: test-command (direct)", color.CyanString("test-type")))) 118 }) 119 }) 120 121 context("SlicesFormatter", func() { 122 123 it("formats contents", func() { 124 slices := []libcnb.Slice{{}, {}} 125 126 Expect(internal.SlicesFormatter(slices).String()). 127 To(Equal("2 application slices")) 128 }) 129 }) 130 131 context("StoreFormatter", func() { 132 133 it("formats contents", func() { 134 store := libcnb.Store{ 135 Metadata: map[string]interface{}{ 136 "test-key-1": "test-value-1", 137 "test-key-2": "test-value-2", 138 }, 139 } 140 141 Expect(internal.StoreFormatter(store).String()). 142 To(Equal("Writing persistent metadata: test-key-1, test-key-2")) 143 }) 144 145 }) 146 }