| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var nopt = require("../") |
| 2 | , test = require('tap').test |
| 3 | |
| 4 | |
| 5 | test("passing a string results in a string", function (t) { |
| 6 | var parsed = nopt({ key: String }, {}, ["--key", "myvalue"], 0) |
| 7 | t.same(parsed.key, "myvalue") |
| 8 | t.end() |
| 9 | }) |
| 10 | |
| 11 | // https://github.com/npm/nopt/issues/31 |
| 12 | test("Empty String results in empty string, not true", function (t) { |
| 13 | var parsed = nopt({ empty: String }, {}, ["--empty"], 0) |
| 14 | t.same(parsed.empty, "") |
| 15 | t.end() |
| 16 | }) |
| 17 | |
| 18 | test("~ path is resolved to $HOME", function (t) { |
| 19 | var path = require("path") |
| 20 | if (!process.env.HOME) process.env.HOME = "/tmp" |
| 21 | var parsed = nopt({key: path}, {}, ["--key=~/val"], 0) |
| 22 | t.same(parsed.key, path.resolve(process.env.HOME, "val")) |
| 23 | t.end() |
| 24 | }) |
| 25 | |
| 26 | // https://github.com/npm/nopt/issues/24 |
| 27 | test("Unknown options are not parsed as numbers", function (t) { |
| 28 | var parsed = nopt({"parse-me": Number}, null, ['--leave-as-is=1.20', '--parse-me=1.20'], 0) |
| 29 | t.equal(parsed['leave-as-is'], '1.20') |
| 30 | t.equal(parsed['parse-me'], 1.2) |
| 31 | t.end() |
| 32 | }); |
| 33 | |
| 34 | // https://github.com/npm/nopt/issues/48 |
| 35 | test("Check types based on name of type", function (t) { |
| 36 | var parsed = nopt({"parse-me": {name: "Number"}}, null, ['--parse-me=1.20'], 0) |
| 37 | t.equal(parsed['parse-me'], 1.2) |
| 38 | t.end() |
| 39 | }) |
| 40 | |
| 41 | |
| 42 | test("Missing types are not parsed", function (t) { |
| 43 | var parsed = nopt({"parse-me": {}}, null, ['--parse-me=1.20'], 0) |
| 44 | //should only contain argv |
| 45 | t.equal(Object.keys(parsed).length, 1) |
| 46 | t.end() |
| 47 | }) |
| 48 | |
| 49 | test("Types passed without a name are not parsed", function (t) { |
| 50 | var parsed = nopt({"parse-me": {}}, {}, ['--parse-me=1.20'], 0) |
| 51 | //should only contain argv |
| 52 | t.equal(Object.keys(parsed).length, 1) |
| 53 | t.end() |
| 54 | }) |
| 55 | |
| 56 | test("other tests", function (t) { |
| 57 | |
| 58 | var util = require("util") |
| 59 | , Stream = require("stream") |
| 60 | , path = require("path") |
| 61 | , url = require("url") |
| 62 | |
| 63 | , shorthands = |
| 64 | { s : ["--loglevel", "silent"] |
| 65 | , d : ["--loglevel", "info"] |
| 66 | , dd : ["--loglevel", "verbose"] |
| 67 | , ddd : ["--loglevel", "silly"] |
| 68 | , noreg : ["--no-registry"] |
| 69 | , reg : ["--registry"] |
| 70 | , "no-reg" : ["--no-registry"] |
| 71 | , silent : ["--loglevel", "silent"] |
| 72 | , verbose : ["--loglevel", "verbose"] |
| 73 | , h : ["--usage"] |
| 74 | , H : ["--usage"] |
| 75 | , "?" : ["--usage"] |
| 76 | , help : ["--usage"] |
| 77 | , v : ["--version"] |
| 78 | , f : ["--force"] |
| 79 | , desc : ["--description"] |
| 80 | , "no-desc" : ["--no-description"] |
| 81 | , "local" : ["--no-global"] |
| 82 | , l : ["--long"] |
| 83 | , p : ["--parseable"] |
| 84 | , porcelain : ["--parseable"] |
| 85 | , g : ["--global"] |
| 86 | } |
| 87 | |
| 88 | , types = |
| 89 | { aoa: Array |
| 90 | , nullstream: [null, Stream] |
| 91 | , date: Date |
| 92 | , str: String |
| 93 | , browser : String |
| 94 | , cache : path |
| 95 | , color : ["always", Boolean] |
| 96 | , depth : Number |
| 97 | , description : Boolean |
| 98 | , dev : Boolean |
| 99 | , editor : path |
| 100 | , force : Boolean |
| 101 | , global : Boolean |
| 102 | , globalconfig : path |
| 103 | , group : [String, Number] |
| 104 | , gzipbin : String |
| 105 | , logfd : [Number, Stream] |
| 106 | , loglevel : ["silent","win","error","warn","info","verbose","silly"] |
| 107 | , long : Boolean |
| 108 | , "node-version" : [false, String] |
| 109 | , npaturl : url |
| 110 | , npat : Boolean |
| 111 | , "onload-script" : [false, String] |
| 112 | , outfd : [Number, Stream] |
| 113 | , parseable : Boolean |
| 114 | , pre: Boolean |
| 115 | , prefix: path |
| 116 | , proxy : url |
| 117 | , "rebuild-bundle" : Boolean |
| 118 | , registry : url |
| 119 | , searchopts : String |
| 120 | , searchexclude: [null, String] |
| 121 | , shell : path |
| 122 | , t: [Array, String] |
| 123 | , tag : String |
| 124 | , tar : String |
| 125 | , tmp : path |
| 126 | , "unsafe-perm" : Boolean |
| 127 | , usage : Boolean |
| 128 | , user : String |
| 129 | , username : String |
| 130 | , userconfig : path |
| 131 | , version : Boolean |
| 132 | , viewer: path |
| 133 | , _exit : Boolean |
| 134 | , path: path |
| 135 | } |
| 136 | |
| 137 | ; [["-v", {version:true}, []] |
| 138 | ,["---v", {version:true}, []] |
| 139 | ,["ls -s --no-reg connect -d", |
| 140 | {loglevel:"info",registry:null},["ls","connect"]] |
| 141 | ,["ls ---s foo",{loglevel:"silent"},["ls","foo"]] |
| 142 | ,["ls --registry blargle", {}, ["ls"]] |
| 143 | ,["--no-registry", {registry:null}, []] |
| 144 | ,["--no-color true", {color:false}, []] |
| 145 | ,["--no-color false", {color:true}, []] |
| 146 | ,["--no-color", {color:false}, []] |
| 147 | ,["--color false", {color:false}, []] |
| 148 | ,["--color --logfd 7", {logfd:7,color:true}, []] |
| 149 | ,["--color=true", {color:true}, []] |
| 150 | ,["--logfd=10", {logfd:10}, []] |
| 151 | ,["--tmp=/tmp -tar=gtar",{tmp:"/tmp",tar:"gtar"},[]] |
| 152 | ,["--tmp=tmp -tar=gtar", |
| 153 | {tmp:path.resolve(process.cwd(), "tmp"),tar:"gtar"},[]] |
| 154 | ,["--logfd x", {}, []] |
| 155 | ,["a -true -- -no-false", {true:true},["a","-no-false"]] |
| 156 | ,["a -no-false", {false:false},["a"]] |
| 157 | ,["a -no-no-true", {true:true}, ["a"]] |
| 158 | ,["a -no-no-no-false", {false:false}, ["a"]] |
| 159 | ,["---NO-no-No-no-no-no-nO-no-no"+ |
| 160 | "-No-no-no-no-no-no-no-no-no"+ |
| 161 | "-no-no-no-no-NO-NO-no-no-no-no-no-no"+ |
| 162 | "-no-body-can-do-the-boogaloo-like-I-do" |
| 163 | ,{"body-can-do-the-boogaloo-like-I-do":false}, []] |
| 164 | ,["we are -no-strangers-to-love "+ |
| 165 | "--you-know=the-rules --and=so-do-i "+ |
| 166 | "---im-thinking-of=a-full-commitment "+ |
| 167 | "--no-you-would-get-this-from-any-other-guy "+ |
| 168 | "--no-gonna-give-you-up "+ |
| 169 | "-no-gonna-let-you-down=true "+ |
| 170 | "--no-no-gonna-run-around false "+ |
| 171 | "--desert-you=false "+ |
| 172 | "--make-you-cry false "+ |
| 173 | "--no-tell-a-lie "+ |
| 174 | "--no-no-and-hurt-you false" |
| 175 | ,{"strangers-to-love":false |
| 176 | ,"you-know":"the-rules" |
| 177 | ,"and":"so-do-i" |
| 178 | ,"you-would-get-this-from-any-other-guy":false |
| 179 | ,"gonna-give-you-up":false |
| 180 | ,"gonna-let-you-down":false |
| 181 | ,"gonna-run-around":false |
| 182 | ,"desert-you":false |
| 183 | ,"make-you-cry":false |
| 184 | ,"tell-a-lie":false |
| 185 | ,"and-hurt-you":false |
| 186 | },["we", "are"]] |
| 187 | ,["-t one -t two -t three" |
| 188 | ,{t: ["one", "two", "three"]} |
| 189 | ,[]] |
| 190 | ,["-t one -t null -t three four five null" |
| 191 | ,{t: ["one", "null", "three"]} |
| 192 | ,["four", "five", "null"]] |
| 193 | ,["-t foo" |
| 194 | ,{t:["foo"]} |
| 195 | ,[]] |
| 196 | ,["--no-t" |
| 197 | ,{t:["false"]} |
| 198 | ,[]] |
| 199 | ,["-no-no-t" |
| 200 | ,{t:["true"]} |
| 201 | ,[]] |
| 202 | ,["-aoa one -aoa null -aoa 100" |
| 203 | ,{aoa:["one", null, '100']} |
| 204 | ,[]] |
| 205 | ,["-str 100" |
| 206 | ,{str:"100"} |
| 207 | ,[]] |
| 208 | ,["--color always" |
| 209 | ,{color:"always"} |
| 210 | ,[]] |
| 211 | ,["--no-nullstream" |
| 212 | ,{nullstream:null} |
| 213 | ,[]] |
| 214 | ,["--nullstream false" |
| 215 | ,{nullstream:null} |
| 216 | ,[]] |
| 217 | ,["--notadate=2011-01-25" |
| 218 | ,{notadate: "2011-01-25"} |
| 219 | ,[]] |
| 220 | ,["--date 2011-01-25" |
| 221 | ,{date: new Date("2011-01-25")} |
| 222 | ,[]] |
| 223 | ,["-cl 1" |
| 224 | ,{config: true, length: 1} |
| 225 | ,[] |
| 226 | ,{config: Boolean, length: Number, clear: Boolean} |
| 227 | ,{c: "--config", l: "--length"}] |
| 228 | ,["--acount bla" |
| 229 | ,{"acount":true} |
| 230 | ,["bla"] |
| 231 | ,{account: Boolean, credentials: Boolean, options: String} |
| 232 | ,{a:"--account", c:"--credentials",o:"--options"}] |
| 233 | ,["--clear" |
| 234 | ,{clear:true} |
| 235 | ,[] |
| 236 | ,{clear:Boolean,con:Boolean,len:Boolean,exp:Boolean,add:Boolean,rep:Boolean} |
| 237 | ,{c:"--con",l:"--len",e:"--exp",a:"--add",r:"--rep"}] |
| 238 | ,["--file -" |
| 239 | ,{"file":"-"} |
| 240 | ,[] |
| 241 | ,{file:String} |
| 242 | ,{}] |
| 243 | ,["--file -" |
| 244 | ,{"file":true} |
| 245 | ,["-"] |
| 246 | ,{file:Boolean} |
| 247 | ,{}] |
| 248 | ,["--path" |
| 249 | ,{"path":null} |
| 250 | ,[]] |
| 251 | ,["--path ." |
| 252 | ,{"path":process.cwd()} |
| 253 | ,[]] |
| 254 | ].forEach(function (test) { |
| 255 | var argv = test[0].split(/\s+/) |
| 256 | , opts = test[1] |
| 257 | , rem = test[2] |
| 258 | , actual = nopt(test[3] || types, test[4] || shorthands, argv, 0) |
| 259 | , parsed = actual.argv |
| 260 | delete actual.argv |
| 261 | for (var i in opts) { |
| 262 | var e = JSON.stringify(opts[i]) |
| 263 | , a = JSON.stringify(actual[i] === undefined ? null : actual[i]) |
| 264 | if (e && typeof e === "object") { |
| 265 | t.deepEqual(e, a) |
| 266 | } else { |
| 267 | t.equal(e, a) |
| 268 | } |
| 269 | } |
| 270 | t.deepEqual(rem, parsed.remain) |
| 271 | }) |
| 272 | t.end() |
| 273 | }) |