github.com/tomwright/dasel@v1.27.3/internal/command/root_delete_test.go (about) 1 package command_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/tomwright/dasel/internal/command" 7 "io" 8 "strings" 9 "testing" 10 ) 11 12 func TestRootCMD_Delete(t *testing.T) { 13 t.Run("InvalidFile", expectErr( 14 []string{"delete", "-f", "bad.json", "-s", "x"}, 15 "could not open input file", 16 )) 17 t.Run("MissingParser", expectErr( 18 []string{"delete", "-s", "x"}, 19 "parser flag required when reading from stdin", 20 )) 21 22 t.Run("InvalidSingleSelector", expectErrFromInput( 23 `{"name": "Tom"}`, 24 []string{"delete", "-p", "json", "-s", "[-]"}, 25 "invalid index: -", 26 )) 27 t.Run("InvalidMultiSelector", expectErrFromInput( 28 `{"name": "Tom"}`, 29 []string{"delete", "-p", "json", "-m", "-s", "[-]"}, 30 "invalid index: -", 31 )) 32 t.Run("ValueNotFound", expectErrFromInput( 33 `{"name": "Tom"}`, 34 []string{"delete", "-p", "json", "-s", ".age"}, 35 "no value found for selector: .age", 36 )) 37 t.Run("InvalidRootNode", expectErrFromInput( 38 ``, 39 []string{"delete", "-p", "json", "-s", ".age"}, 40 "no value found for selector: .age", 41 )) 42 t.Run("InvalidRootNodeMulti", expectErrFromInput( 43 ``, 44 []string{"delete", "-p", "json", "-m", "-s", ".age"}, 45 "no value found for selector: .age", 46 )) 47 } 48 49 func deleteTest(in string, parser string, selector string, output string, expErr error, additionalArgs ...string) func(t *testing.T) { 50 return deleteTestCheck(in, parser, selector, func(out string) error { 51 if out != output { 52 return fmt.Errorf("expected %v, got %v", output, out) 53 } 54 return nil 55 }, expErr, additionalArgs...) 56 } 57 58 func deleteTestCheck(in string, parser string, selector string, checkFn func(out string) error, expErr error, additionalArgs ...string) func(t *testing.T) { 59 return func(t *testing.T) { 60 cmd := command.NewRootCMD() 61 outputBuffer := bytes.NewBuffer([]byte{}) 62 63 args := []string{ 64 "delete", "-p", parser, 65 } 66 if additionalArgs != nil { 67 args = append(args, additionalArgs...) 68 } 69 args = append(args, selector) 70 71 cmd.SetOut(outputBuffer) 72 cmd.SetIn(strings.NewReader(in)) 73 cmd.SetArgs(args) 74 75 err := cmd.Execute() 76 77 if expErr == nil && err != nil { 78 t.Errorf("expected err %v, got %v", expErr, err) 79 return 80 } 81 if expErr != nil && err == nil { 82 t.Errorf("expected err %v, got %v", expErr, err) 83 return 84 } 85 if expErr != nil && err != nil && err.Error() != expErr.Error() { 86 t.Errorf("expected err %v, got %v", expErr, err) 87 return 88 } 89 90 output, err := io.ReadAll(outputBuffer) 91 if err != nil { 92 t.Errorf("unexpected error reading output buffer: %s", err) 93 return 94 } 95 96 if err := checkFn(string(output)); err != nil { 97 t.Errorf("unexpected output: %s", err) 98 } 99 } 100 } 101 102 func TestRootCmd_Delete_JSON(t *testing.T) { 103 t.Run("RootNodeObject", deleteTest(`{ 104 "email": "tom@wright.com", 105 "name": "Tom" 106 }`, "json", ".", newline(`{}`), nil)) 107 108 t.Run("RootNodeObjectMulti", deleteTest(`{ 109 "email": "tom@wright.com", 110 "name": "Tom" 111 }`, "json", ".", newline(`{}`), nil, "-m")) 112 113 t.Run("RootNodeArray", deleteTest(`["a", "b", "c"]`, "json", ".", 114 newline(`[]`), nil)) 115 116 t.Run("RootNodeArrayMulti", deleteTest(`["a", "b", "c"]`, "json", ".", 117 newline(`[]`), nil, "-m")) 118 119 t.Run("RootNodeUnknown", deleteTest(`false`, "json", ".", 120 newline(`{}`), nil)) 121 122 t.Run("RootNodeUnknownMulti", deleteTest(`false`, "json", ".", 123 newline(`{}`), nil, "-m")) 124 125 t.Run("Property", deleteTest(`{ 126 "email": "tom@wright.com", 127 "name": "Tom" 128 }`, "json", ".email", newline(`{ 129 "name": "Tom" 130 }`), nil)) 131 132 t.Run("PropertyCompact", deleteTest(`{ 133 "email": "tom@wright.com", 134 "name": "Tom" 135 }`, "json", ".email", newline(`{"name":"Tom"}`), nil, "-c")) 136 137 t.Run("Index", deleteTest(`{ 138 "colours": ["blue", "green", "red"], 139 "name": "Tom" 140 }`, "json", ".colours.[1]", newline(`{ 141 "colours": [ 142 "blue", 143 "red" 144 ], 145 "name": "Tom" 146 }`), nil)) 147 148 t.Run("AnyIndex", deleteTest(`{ 149 "colours": ["blue", "green", "red"], 150 "name": "Tom" 151 }`, "json", ".colours.[*]", newline(`{ 152 "colours": [], 153 "name": "Tom" 154 }`), nil, "-m")) 155 156 t.Run("RootObject", deleteTest(`{ 157 "name": "Tom" 158 }`, "json", ".", newline(`{}`), nil)) 159 t.Run("RootObjectMulti", deleteTest(`{ 160 "name": "Tom" 161 }`, "json", ".", newline(`{}`), nil, "-m")) 162 163 t.Run("RootArray", deleteTest(`[1, 2, 3]`, "json", ".", newline(`[]`), nil)) 164 t.Run("RootArrayMulti", deleteTest(`[1, 2, 3]`, "json", ".", newline(`[]`), nil, "-m")) 165 166 t.Run("DeleteStringEscapeHTMLOn", deleteTest(`{ 167 "name": "Tom", 168 "user": "Tom <contact@tomwright.me>" 169 } 170 `, "json", `.name`, `{ 171 "user": "Tom \u003ccontact@tomwright.me\u003e" 172 } 173 `, nil, "--escape-html=true")) 174 175 t.Run("DeleteStringEscapeHTMLOff", deleteTest(`{ 176 "name": "Tom", 177 "user": "Tom <contact@tomwright.me>" 178 } 179 `, "json", `.name`, `{ 180 "user": "Tom <contact@tomwright.me>" 181 } 182 `, nil, "--escape-html=false")) 183 }