1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
# -*- tcl -*-
# This file contains the tests for the cmdline.tcl file.
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands. Sourcing this file into Tcl runs the tests and
# generates output for errors. No output means no errors were found.
#
# Copyright (c) 1999 by Ajuba Solutions.
# All rights reserved.
# -------------------------------------------------------------------------
source [file join \
[file dirname [file dirname [file join [pwd] [info script]]]] \
devtools testutilities.tcl]
testsNeedTcl 8.5
testsNeedTcltest 1.0
testing {
useLocal cmdline.tcl cmdline
}
# -------------------------------------------------------------------------
set cmdLineFile [localPath cmdline.tcl]
set argv0 "argv0"
# ---------------------------------------------------
# cmdline::getopt
test cmdline-1.1 {cmdline::getopt} {
catch {unset opt}
catch {unset arg}
set argList {}
list [cmdline::getopt argList {a} opt arg] $argList $opt $arg
} {0 {} {} {}}
test cmdline-1.2 {cmdline::getopt, multiple options} {
catch {unset opt}
catch {unset arg}
set argList {}
list [cmdline::getopt argList {a b.arg c} opt arg] $argList $opt $arg
} {0 {} {} {}}
test cmdline-1.3 {cmdline::getopt, -- option} {
catch {unset opt}
catch {unset arg}
set argList {-- -a}
list [cmdline::getopt argList {a} opt arg] $argList $opt $arg
} {0 -a {} {}}
test cmdline-1.4 {cmdline::getopt, non dash option} {
catch {unset opt}
catch {unset arg}
set argList {b -a}
list [cmdline::getopt argList {a} opt arg] $argList $opt $arg
} {0 {b -a} {} {}}
test cmdline-1.5 {cmdline::getopt, simple option} {
catch {unset opt}
catch {unset arg}
set argList {-a b}
list [cmdline::getopt argList {a} opt arg] $argList $opt $arg
} {1 b a 1}
test cmdline-1.6 {cmdline::getopt, multiple letter option} {
catch {unset opt}
catch {unset arg}
set argList {-foo b}
list [cmdline::getopt argList {foo} opt arg] $argList $opt $arg
} {1 b foo 1}
test cmdline-1.7 {cmdline::getopt, multiple letter option, no abbreviations} {
catch {unset opt}
catch {unset arg}
set argList {-f b}
list [cmdline::getopt argList {foo} opt arg] $argList $opt $arg
} {-1 {-f b} f {Illegal option "-f"}}
test cmdline-1.8 {cmdline::getopt, option with argument} {
catch {unset opt}
catch {unset arg}
set argList {-foo bar baz}
list [cmdline::getopt argList {foo.arg} opt arg] $argList $opt $arg
} {1 baz foo bar}
test cmdline-1.9 {cmdline::getopt, option with argument, missing arg} {
catch {unset opt}
catch {unset arg}
set argList {-foo}
list [cmdline::getopt argList {foo.arg} opt arg] $argList $opt $arg
} {-1 {} foo {Option "foo" requires an argument}}
test cmdline-1.10 {cmdline::getopt, unknown option} {
catch {unset opt}
catch {unset arg}
set argList {-bar}
list [cmdline::getopt argList {foo.arg} opt arg] $argList $opt $arg
} {-1 -bar bar {Illegal option "-bar"}}
test cmdline-1.11 {cmdline::getopt, multiple options} {
catch {unset opt}
catch {unset arg}
set argList {-foo}
list [cmdline::getopt argList {a.arg b foo c.arg} opt arg] $argList $opt $arg
} {1 {} foo 1}
test cmdline-1.12 {cmdline::getopt, option with argument, -o=v syntax} {
catch {unset opt}
catch {unset arg}
set argList {-foo=bar baz}
list [cmdline::getopt argList {foo.arg} opt arg] $argList $opt $arg
} {1 baz foo bar}
test cmdline-1.13 {cmdline::getopt, option with argument, --o=v syntax} {
catch {unset opt}
catch {unset arg}
set argList {--foo=bar baz}
list [cmdline::getopt argList {foo.arg} opt arg] $argList $opt $arg
} {1 baz foo bar}
# cmdline::getoptions
test cmdline-2.1 {cmdline::getoptions} {
set argList {foo}
list [cmdline::getoptions argList {}] $argList
} {{} foo}
test cmdline-2.2 {cmdline::getoptions, secret flag} {
set argList {-foo}
list [cmdline::getoptions argList {{foo.secret}}] $argList
} {{foo 1} {}}
test cmdline-2.3 {cmdline::getoptions, normal flag} {
set argList {-foo}
list [cmdline::getoptions argList {{foo}}] $argList
} {{foo 1} {}}
test cmdline-2.4 {cmdline::getoptions, flag with arg} {
set argList {-foo bar}
list [cmdline::getoptions argList {{foo.arg}}] $argList
} {{foo bar} {}}
test cmdline-2.5 {cmdline::getoptions, missing flag with arg, default value} {
set argList {}
list [cmdline::getoptions argList {{foo.arg blat}}] $argList
} {{foo blat} {}}
test cmdline-2.6 {cmdline::getoptions, flag with arg, default value} {
set argList {-foo bar}
list [cmdline::getoptions argList {{foo.arg blat}}] $argList
} {{foo bar} {}}
test cmdline-2.7 {cmdline::getoptions, multiple flags with arg, default value} {
set argList {}
list [dictsort [cmdline::getoptions argList {{foo.arg blat} {a.arg b}}]] $argList
} {{a b foo blat} {}}
test cmdline-2.8 {cmdline::getoptions, errors} {
set argList {-a -foo}
list [catch {cmdline::getoptions argList {{foo.arg blat} a}} msg] $msg $argList
} [list 1 "[cmdline::getArgv0] options:
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-2.9 {cmdline::getoptions, errors} {
set argList {-a -?}
list [catch {cmdline::getoptions argList {{foo.arg blat} a}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-2.10 {cmdline::getoptions, errors} {
set argList {-help}
list [catch {cmdline::getoptions argList {{foo.arg blat} a}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-2.11 {cmdline::getoptions, usage string in errors} {
set argList {-help}
list [catch {cmdline::getoptions argList {{foo.arg blat} a} {testing}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] testing
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-2.12 {cmdline::getoptions, bug 3189786} {
set argList {-help}
list [catch {cmdline::getoptions argList {myarg a} {testing}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] testing
-myarg
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
# cmdline::usage
test cmdline-3.1 {cmdline::usage,hidden options} {
set argList {-help}
list [catch {cmdline::getoptions argList {{foo.secret blat} a}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-3.2 {cmdline::usage, with & without arg} {
set argList {-help}
list [catch {cmdline::getoptions argList \
{{foo.arg blat testing} {a {} {line 2}}}} msg] $msg $argList
} [list 1 "[cmdline::getArgv0] options:
-foo value testing <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-3.3 {cmdline::usage, bug 3189786} {
set argList {-help}
list [catch {cmdline::getoptions argList {{mysecret blat} a}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-mysecret blat
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-3.4 {cmdline::usage, long options} {
set argList {-help}
list [catch {cmdline::getoptions argList {{mysecret blat} a {very-long-option.arg foobar {A very very long option}}}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-mysecret blat
-a
-very-long-option value A very very long option <foobar>
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
# cmdline::getfiles
# Run the script body in a slave process so we can collect stdout.
proc runGetFilesTest {body} {
set script "source [list $::cmdLineFile]\n"
append script "cd [list $::tcltest::temporaryDirectory]\n"
append script $body
set scriptfile [makeFile $script script]
set f [open "|[list $::tcltest::tcltest $scriptfile]" r]
set result [read $f]
close $f
removeFile script
return $result
}
# Create a directory with some files in it
makeDirectory cmdlineJunk
set foo1 [makeFile {} cmdlineJunk/foo1]
set foo2 [makeFile {} cmdlineJunk/foo2]
set bar3 [makeFile {} cmdlineJunk/bar3]
test cmdline-4.1 {cmdline::getfiles} {pcOnly} {
runGetFilesTest {
cmdline::getfiles {} 0
}
} {}
test cmdline-4.2 {cmdline::getfiles, one pattern} {pcOnly} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {foo*} 0]
puts -nonewline [lsort $result]
exit
}
} [list $foo1 $foo2]
test cmdline-4.3 {cmdline::getfiles, multiple patterns} {pcOnly} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {foo* bar*} 0]
puts -nonewline [lsort $result]
exit
}
} [list $bar3 $foo1 $foo2]
test cmdline-4.4 {cmdline::getfiles, no match} {pcOnly} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {blat* foo*} 0]
puts -nonewline [lsort $result]
exit
}
} "warning: no files match \"blat*\"\n[list $foo1 $foo2]"
test cmdline-4.5 {cmdline::getfiles, quiet} {pcOnly} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {blat* foo*} 1]
puts -nonewline [lsort $result]
exit
}
} [list $foo1 $foo2]
test cmdline-4.6 {cmdline::getfiles, relative paths} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {foo1 foo2} 0]
puts -nonewline [lsort $result]
exit
}
} [list $foo1 $foo2]
test cmdline-4.7 {cmdline::getfiles, absolute paths} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles [list [file join [pwd] foo1]] 0]
puts -nonewline [lsort $result]
exit
}
} [list $foo1]
test cmdline-4.8 {cmdline::getfiles, no match} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {blat foo1} 0]
puts -nonewline [lsort $result]
exit
}
} "warning: no files match \"blat\"\n[list $foo1]"
test cmdline-4.9 {cmdline::getfiles, silent no match} {
runGetFilesTest {
cd cmdlineJunk
set result [cmdline::getfiles {blat foo1} 1]
puts -nonewline [lsort $result]
exit
}
} [list $foo1]
test cmdline-4.10 {cmdline::getfiles, backslashes on windows} {pc} {
runGetFilesTest {
set result [cmdline::getfiles {cmdlineJunk\\foo*} 1]
puts -nonewline [lsort $result]
exit
}
} [list $foo1 $foo2]
# Remove the temporary directory and files from the previous tests
removeFile cmdlineJunk/foo1
removeFile cmdlineJunk/foo2
removeFile cmdlineJunk/bar3
removeDirectory cmdlineJunk
# cmdline::getArgv0
test cmdline-5.1 {cmdline::getArgv0} {
set oldargv0 $argv0
set argv0 "foo"
set result [cmdline::getArgv0]
set argv0 $oldargv0
set result
} foo
test cmdline-5.2 {cmdline::getArgv0} {
set oldargv0 $argv0
set argv0 "foo.exe"
set result [cmdline::getArgv0]
set argv0 $oldargv0
set result
} foo
test cmdline-5.3 {cmdline::getArgv0} {
set oldargv0 $argv0
set argv0 "foo.bin"
set result [cmdline::getArgv0]
set argv0 $oldargv0
set result
} foo
test cmdline-5.4 {cmdline::getArgv0} {
set oldargv0 $argv0
set argv0 "foo.bar.bin"
set result [cmdline::getArgv0]
set argv0 $oldargv0
set result
} foo.bar
test cmdline-5.5 {cmdline::getArgv0} {
set oldargv0 $argv0
set argv0 "/a/b/c/foo"
set result [cmdline::getArgv0]
set argv0 $oldargv0
set result
} foo
# cmdline::getKnownOpt
test cmdline-6.1 {cmdline::getKnownOpt} {
catch {unset opt}
catch {unset arg}
set argList {}
list [cmdline::getKnownOpt argList {a} opt arg] $argList $opt $arg
} {0 {} {} {}}
test cmdline-6.2 {cmdline::getKnownOpt, multiple options} {
catch {unset opt}
catch {unset arg}
set argList {}
list [cmdline::getKnownOpt argList {a b.arg c} opt arg] $argList $opt $arg
} {0 {} {} {}}
test cmdline-6.3 {cmdline::getKnownOpt, -- option} {
catch {unset opt}
catch {unset arg}
set argList {-- -a}
list [cmdline::getKnownOpt argList {a} opt arg] $argList $opt $arg
} {0 -a {} {}}
test cmdline-6.4 {cmdline::getKnownOpt, non dash option} {
catch {unset opt}
catch {unset arg}
set argList {b -a}
list [cmdline::getKnownOpt argList {a} opt arg] $argList $opt $arg
} {0 {b -a} {} {}}
test cmdline-6.5 {cmdline::getKnownOpt, simple option} {
catch {unset opt}
catch {unset arg}
set argList {-a b}
list [cmdline::getKnownOpt argList {a} opt arg] $argList $opt $arg
} {1 b a 1}
test cmdline-6.6 {cmdline::getKnownOpt, multiple letter option} {
catch {unset opt}
catch {unset arg}
set argList {-foo b}
list [cmdline::getKnownOpt argList {foo} opt arg] $argList $opt $arg
} {1 b foo 1}
test cmdline-6.7 {cmdline::getKnownOpt, multiple letter option, no abbreviations} {
catch {unset opt}
catch {unset arg}
set argList {-f b}
list [cmdline::getKnownOpt argList {foo} opt arg] $argList $opt $arg
} {-1 {-f b} f {Illegal option "-f"}}
test cmdline-6.8 {cmdline::getKnownOpt, option with argument} {
catch {unset opt}
catch {unset arg}
set argList {-foo bar baz}
list [cmdline::getKnownOpt argList {foo.arg} opt arg] $argList $opt $arg
} {1 baz foo bar}
test cmdline-6.9 {cmdline::getKnownOpt, option with argument, missing arg} {
catch {unset opt}
catch {unset arg}
set argList {-foo}
list [cmdline::getKnownOpt argList {foo.arg} opt arg] $argList $opt $arg
} {-2 {} foo {Option "foo" requires an argument}}
test cmdline-6.10 {cmdline::getKnownOpt, unknown option} {
catch {unset opt}
catch {unset arg}
set argList {-bar}
list [cmdline::getKnownOpt argList {foo.arg} opt arg] $argList $opt $arg
} {-1 -bar bar {Illegal option "-bar"}}
test cmdline-6.11 {cmdline::getKnownOpt, multiple options} {
catch {unset opt}
catch {unset arg}
set argList {-foo}
list [cmdline::getKnownOpt argList {a.arg b foo c.arg} opt arg] $argList $opt $arg
} {1 {} foo 1}
# cmdline::getKnownOptions
test cmdline-7.1 {cmdline::getKnownOptions} {
set argList {foo}
list [cmdline::getKnownOptions argList {}] $argList
} {{} foo}
test cmdline-7.2 {cmdline::getKnownOptions, secret flag} {
set argList {-foo}
list [cmdline::getKnownOptions argList {{foo.secret}}] $argList
} {{foo 1} {}}
test cmdline-7.3 {cmdline::getKnownOptions, normal flag} {
set argList {-foo}
list [cmdline::getKnownOptions argList {{foo}}] $argList
} {{foo 1} {}}
test cmdline-7.4 {cmdline::getKnownOptions, flag with arg} {
set argList {-foo bar}
list [cmdline::getKnownOptions argList {{foo.arg}}] $argList
} {{foo bar} {}}
test cmdline-7.5 {cmdline::getKnownOptions, missing flag with arg, default value} {
set argList {}
list [cmdline::getKnownOptions argList {{foo.arg blat}}] $argList
} {{foo blat} {}}
test cmdline-7.6 {cmdline::getKnownOptions, flag with arg, default value} {
set argList {-foo bar}
list [cmdline::getKnownOptions argList {{foo.arg blat}}] $argList
} {{foo bar} {}}
test cmdline-7.7 {cmdline::getKnownOptions, multiple flags with arg, default value} {
set argList {}
list [dictsort [cmdline::getKnownOptions argList {{foo.arg blat} {a.arg b}}]] $argList
} {{a b foo blat} {}}
test cmdline-7.8 {cmdline::getKnownOptions, ignore unknown option} {
set argList {-unknown -foo buzz}
list [cmdline::getKnownOptions argList {{foo.arg blat}}] $argList
} {{foo buzz} -unknown}
test cmdline-7.9 {cmdline::getKnownOptions, ignore unknown option} {
set argList {-foo buzz -unknown}
list [cmdline::getKnownOptions argList {{foo.arg blat}}] $argList
} {{foo buzz} -unknown}
test cmdline-7.10 {cmdline::getKnownOptions, ignore unknown option with args} {
set argList {-unknown u1 u2 u3 -foo buzz}
list [cmdline::getKnownOptions argList {{foo.arg blat}}] $argList
} {{foo buzz} {-unknown u1 u2 u3}}
test cmdline-7.11 {cmdline::getKnownOptions, ignore unknown option with args} {
set argList {-foo buzz -unknown u1 u2 u3}
list [cmdline::getKnownOptions argList {{foo.arg blat}}] $argList
} {{foo buzz} {-unknown u1 u2 u3}}
test cmdline-7.12 {cmdline::getKnownOptions, errors} {
set argList {-a -foo}
list [catch {cmdline::getKnownOptions argList {{foo.arg blat} a}} msg] $msg $argList
} [list 1 "[cmdline::getArgv0] options:
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-7.13 {cmdline::getKnownOptions, errors} {
set argList {-a -?}
list [catch {cmdline::getKnownOptions argList {{foo.arg blat} a}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-7.14 {cmdline::getKnownOptions, errors} {
set argList {-help}
list [catch {cmdline::getKnownOptions argList {{foo.arg blat} a}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] options:
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
test cmdline-7.15 {cmdline::getKnownOptions, usage string in errors} {
set argList {-help}
list [catch {cmdline::getKnownOptions argList {{foo.arg blat} a} {testing}} msg] $msg \
$argList
} [list 1 "[cmdline::getArgv0] testing
-foo value <blat>
-a
-- Forcibly stop option processing
-help Print this message
-? Print this message
" {}]
testsuiteCleanup
return
|