github.com/wheelercj/pm2md@v0.0.11/cmd/root_test.go (about) 1 // Copyright 2023 Chris Wheeler 2 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "os" 19 "testing" 20 ) 21 22 func TestArgsFunc(t *testing.T) { 23 tests := []struct { 24 name string 25 input []string 26 }{ 27 {"[]string{\"api.json\"}", []string{"api.json"}}, 28 {"[]string{\"a.json\", \"out.txt\"}", []string{"a.json", "out.txt"}}, 29 {"[]string{\"-\"}", []string{"-"}}, 30 } 31 32 for _, test := range tests { 33 t.Run(test.name, func(t *testing.T) { 34 err := argsFunc(nil, test.input) 35 if err != nil { 36 t.Errorf("argsFunc(nil, %q) = %q, want nil", test.name, err) 37 } 38 }) 39 } 40 } 41 42 func TestArgsFuncWithInvalidArgs(t *testing.T) { 43 tests := []struct { 44 name string 45 input []string 46 }{ 47 {"nil", nil}, 48 {"[]string{\"a.json\", \"b\", \"c\"}", []string{"a.json", "b", "c"}}, 49 {"[]string{\"file.txt\"}", []string{"file.txt"}}, 50 } 51 52 for _, test := range tests { 53 t.Run(test.name, func(t *testing.T) { 54 err := argsFunc(nil, test.input) 55 if err == nil { 56 t.Errorf("argsFunc(nil, %q) = nil, want non-nil error", test.name) 57 } 58 }) 59 } 60 } 61 62 func TestArgsFuncGetTemplate(t *testing.T) { 63 GetDefault = true 64 err := argsFunc(nil, nil) 65 if err != nil { 66 t.Errorf("argsFunc(nil, nil) = %q, want nil", err) 67 } 68 GetDefault = false 69 } 70 71 func TestArgsFuncWithCustomTmplPath(t *testing.T) { 72 CustomTmplPath = "custom.tmpl" 73 err := argsFunc(nil, []string{"api.json"}) 74 if err != nil { 75 t.Errorf("argsFunc(nil, []string{\"api.json\"}) = %q, want nil", err) 76 } 77 CustomTmplPath = "" 78 } 79 80 func TestArgsFuncWithInvalidCustomTmplPath(t *testing.T) { 81 CustomTmplPath = "custom.template" 82 err := argsFunc(nil, []string{"api.json"}) 83 if err == nil { 84 t.Errorf("argsFunc(nil, []string{\"api.json\"}) = nil, want non-nil error") 85 } 86 CustomTmplPath = "" 87 } 88 89 func TestParseInputWithInvalidStatuses(t *testing.T) { 90 jsonPath := "../samples/calendar-API.postman_collection.json" 91 Statuses = "this is not a valid statuses value" 92 destPath, destFile, _, _, err := parseInput(nil, []string{jsonPath}) 93 Statuses = "" 94 if err == nil { 95 t.Error("parseInput(nil, []string{\"\"}) with invalid statuses returned nil error, want non-nil error") 96 defer os.Remove(destPath) 97 defer destFile.Close() 98 } 99 } 100 101 func TestParseInputWithInvalidJsonPath(t *testing.T) { 102 jsonPath := "nonexistent.json" 103 destPath, destFile, _, _, err := parseInput(nil, []string{jsonPath}) 104 if err == nil { 105 t.Errorf("parseInput(nil, []string{%q}) returned nil error, want non-nil error", jsonPath) 106 defer os.Remove(destPath) 107 defer destFile.Close() 108 } 109 } 110 111 func TestParseInputExistingFileError(t *testing.T) { 112 jsonPath := "../samples/calendar-API.postman_collection.json" 113 destPath := "../samples/calendar-API-v1.md" 114 if !FileExists(destPath) { 115 t.Errorf("Test broken. Expected file %q to exist", destPath) 116 return 117 } 118 destPath, destFile, _, _, err := parseInput(nil, []string{jsonPath, destPath}) 119 if err == nil { 120 t.Errorf("parseInput(nil, []string{%q, %q}) returned nil error, want non-nil error", jsonPath, destPath) 121 defer os.Remove(destPath) 122 defer destFile.Close() 123 } 124 } 125 126 func TestLoadTmplDefault(t *testing.T) { 127 tmplName, tmplStr, err := loadTmpl("") 128 if err != nil { 129 t.Error(err) 130 return 131 } 132 if tmplName != defaultTmplName { 133 t.Errorf("loadTmpl(\"\") returned template name %q, want %q", tmplName, defaultTmplName) 134 } 135 err = AssertNoDiff(tmplStr, defaultTmplStr, "\r\n") 136 if err != nil { 137 t.Errorf("AssertNoDiff returned error %q, want nil error", err) 138 } 139 } 140 141 func TestLoadTmplCustom(t *testing.T) { 142 customTmplPath := "../samples/custom.tmpl" 143 ansName, ansTmplStr, err := loadTmpl(customTmplPath) 144 if err != nil { 145 t.Error(err) 146 return 147 } 148 wantName := "custom.tmpl" 149 customBytes, err := os.ReadFile(customTmplPath) 150 if err != nil { 151 t.Error(err) 152 return 153 } 154 wantTmplStr := string(customBytes) 155 156 if ansName != wantName { 157 t.Errorf("loadTmpl(\"../samples/custom.tmpl\") returned template name %q, want %q", ansName, wantName) 158 } 159 err = AssertNoDiff(ansTmplStr, wantTmplStr, "\r\n") 160 if err != nil { 161 t.Errorf("AssertNoDiff returned error %q, want nil error", err) 162 } 163 } 164 165 func TestLoadTmplNonexistent(t *testing.T) { 166 tmplName, tmplStr, err := loadTmpl("nonexistent.tmpl") 167 if err == nil { 168 t.Errorf("loadTmpl(\"nonexistent.tmpl\") = (%q, len %d template, nil), want non-nil error", tmplName, len(tmplStr)) 169 } 170 }