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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
r"""
This file contains some tests that Sage command line options actually
do something.
We test the following command line options:
test.py
/path/to/test.py
test.sage
/path/to/test.sage
test.spyx
/path/to/test.spyx
--advanced
-c
--cython
--dev
--ecl
--gap
--gdb
--gp
-h
--help
--info
--ipython
--kash
--lisp
--maxima
--min
--mwrank
--preparse
--python
-q
--R
--root
--rst2txt
--rst2sws
--sh
--singular
--sqlite3
--standard
--startuptime
-t
-v
--zzfoobar (illegal option)
AUTHORS:
- Jeroen Demeyer (2010-11-20): initial version (#10300)
"""
from subprocess import *
import os
import select
def test_executable(args, input="", timeout=100.0, **kwds):
r"""
Run the program defined by ``args`` using the string ``input`` on
the standard input.
INPUT:
- ``args`` -- a list of program arguments, the first being the
executable.
- ``input`` -- a string serving as standard input. Usually, this
should end with a newline.
- ``timeout`` -- if the program produces no output for ``timeout``
seconds, a RuntimeError is raised.
- ``**kwds`` -- Additional keyword arguments passed to the
:class:`Popen` constructor.
OUTPUT: a tuple ``(out, err, ret)`` with the standard output,
standard error and exitcode of the program run.
EXAMPLES::
sage: from sage.tests.cmdline import test_executable
sage: (out, err, ret) = test_executable(["cat"], "Hello World!")
sage: out
'Hello World!'
sage: err
''
sage: ret
0
We test the timeout option::
sage: (out, err, ret) = test_executable(["sleep", "1"], timeout=0.1)
Traceback (most recent call last):
...
RuntimeError: timeout in test_executable()
TESTS:
Run Sage itself with various options::
sage: (out, err, ret) = test_executable(["sage"])
sage: out.find(version()) >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage"], "3^33\n")
sage: out.find(version()) >= 0
True
sage: out.find("5559060566555523") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "-q"], "3^33\n")
sage: out.find(version()) >= 0
False
sage: out.find("5559060566555523") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "-c", "print(3^33)"])
sage: print(out)
5559060566555523
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--min", "-c", "print(3^33)"])
sage: print(out)
5559060566555523
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--startuptime"])
sage: out.find("Slowest module import") >= 0
True
sage: err
''
sage: ret
0
Test help::
sage: (out, err, ret) = test_executable(["sage", "-h"])
sage: out.find("Optional arguments:") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--help"])
sage: out.find("Optional arguments:") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--advanced"])
sage: out.find("search through the Sage documentation") >= 0
True
sage: err
''
sage: ret
0
Basic information about the Sage installation::
sage: (out, err, ret) = test_executable(["sage", "-v"])
sage: out.find(version()) >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--root"])
sage: len(out) >= 2 # at least one character + newline
True
sage: err
''
sage: ret
0
Test ``sage-run`` on a Python file, both with an absolute and with a relative path::
sage: dir = tmp_dir(); name = 'python_test_file.py'
sage: fullname = os.path.join(dir, name)
sage: F = open(fullname, 'w')
sage: F.write("print(3^33)\n")
sage: F.close()
sage: (out, err, ret) = test_executable(["sage", fullname])
sage: print(out)
34
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", name], cwd=dir)
sage: print(out)
34
sage: err
''
sage: ret
0
The same as above, but now with a ``.sage`` file. This indirectly
also tests the preparser::
sage: dir = tmp_dir(); name = 'sage_test_file.sage'
sage: fullname = os.path.join(dir, name)
sage: F = open(fullname, 'w')
sage: F.write("k.<a> = GF(5^3); print(a^124)\n")
sage: F.close()
sage: (out, err, ret) = test_executable(["sage", fullname])
sage: print(out)
1
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", name], cwd=dir)
sage: print(out)
1
sage: err
''
sage: ret
0
Test running a ``.spyx`` file::
sage: dir = tmp_dir(); name = 'sage_test_file.spyx'
sage: fullname = os.path.join(dir, name)
sage: F = open(fullname, 'w')
sage: F.write("from sage.rings.integer cimport Integer\ncdef long i, s = 0\nsig_on()\nfor i in range(1000): s += i\nsig_off()\nprint(Integer(s))")
sage: F.close()
sage: (out, err, ret) = test_executable(["sage", fullname])
sage: print(out)
499500
sage: err
'Compiling ...spyx...'
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", name], cwd=dir)
sage: print(out)
499500
sage: err
'Compiling ...spyx...'
sage: ret
0
Testing ``sage --preparse FILE`` and ``sage -t FILE``. First create
a file and preparse it::
sage: s = "# -*- coding: utf-8 -*-\n'''This is a test file.\nAnd I am its doctest'''\ndef my_add(a):\n '''\n Add 2 to a.\n\n EXAMPLES::\n\n sage: my_add(2)\n 4\n '''\n return a + 2\n"
sage: script = os.path.join(tmp_dir(), 'my_script.sage')
sage: script_py = script + '.py'
sage: F = open(script, 'w')
sage: F.write(s)
sage: F.close()
sage: (out, err, ret) = test_executable(["sage", "--preparse", script])
sage: ret
0
sage: os.path.isfile(script_py)
True
Now test my_script.sage and the preparsed version my_script.sage.py::
sage: (out, err, ret) = test_executable(["sage", "-t", script])
sage: ret
0
sage: out.find("All tests passed!") >= 0
True
sage: (out, err, ret) = test_executable(["sage", "-t", script_py])
sage: ret
0
sage: out.find("All tests passed!") >= 0
True
Test that the coding line and doctest are preserved::
sage: Fpy = open(script_py, "r")
sage: Fpy.readline()
'# -*- coding: utf-8 -*-\n'
sage: Fpy.readline()
"'''This is a test file.\n"
sage: Fpy.readline()
"And I am its doctest'''\n"
Now for a file which should fail tests::
sage: s = s.replace('4', '5') # (2+2 != 5)
sage: F = open(script, 'w')
sage: F.write(s)
sage: F.close()
sage: (out, err, ret) = test_executable(["sage", "-t", script])
sage: ret
1
sage: out.find("1 item had failures:") >= 0
True
Test ``sage -t --debug -p 2`` on a ReST file, the ``-p 2`` should
be ignored. In Pdb, we run the ``help`` command::
sage: s = "::\n\n sage: assert True == False\n sage: 2 + 2\n 5"
sage: script = tmp_filename(ext='.rst')
sage: F = open(script, 'w')
sage: F.write(s)
sage: F.close()
sage: (out, err, ret) = test_executable([
....: "sage", "-t", "--debug", "-p", "2", "--warn-long", "0", script], "help")
sage: print(out)
Debugging requires single-threaded operation, setting number of threads to 1.
Running doctests with ID...
Doctesting 1 file.
sage -t ...
**********************************************************************
File "...", line 3, in ...
Failed example:
assert True == False
Exception raised:
Traceback (most recent call last):
...
AssertionError
> <doctest ...>(1)<module>()
-> assert True == False
(Pdb)
Documented commands (type help <topic>):
========================================
...
**********************************************************************
File "...", line 4, in ...
Failed example:
2 + 2
Expected:
5
Got:
4
**********************************************************************
Previously executed commands:
s...: assert True == False
In [1]:
<BLANKLINE>
Returning to doctests...
**********************************************************************
1 item had failures:
2 of 3 in ...
[2 tests, 2 failures, ...]
...
sage: ret
1
# FIXME: this is related to upstream http://bugs.python.org/issue16202
# Check that Sage refuses to run doctests from a directory whose
# permissions are too loose. We create a world-writable directory
# inside a safe temporary directory to test this::
# sage: d = os.path.join(tmp_dir(), "test")
# sage: os.mkdir(d)
# sage: os.chmod(d, 0o777)
# sage: (out, err, ret) = test_executable(["sage", "-t", "nonexisting.py"], cwd=d)
# sage: print(err)
# Traceback (most recent call last):
# ...
# RuntimeError: refusing to run doctests...
# sage: (out, err, ret) = test_executable(["sage", "-tp", "1", "nonexisting.py"], cwd=d)
# sage: print(err)
# Traceback (most recent call last):
# ...
# RuntimeError: refusing to run doctests...
Test external programs being called by Sage::
sage: (out, err, ret) = test_executable(["sage", "--sh"], "echo Hello World\nexit 42\n")
sage: out.find("Hello World\n") >= 0
True
sage: ret
42
sage: (out, err, ret) = test_executable(["sage", "--sh", "-c", "echo Hello World; exit 42"])
sage: out.find("Hello World\n") >= 0
True
sage: ret
42
sage: (out, err, ret) = test_executable(["sage", "--ipython"], "\n3**33\n")
sage: out.find("5559060566555523") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--python"], "print(3^33)\n")
sage: out
'34\n'
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--cython"])
sage: print(err)
Cython (http://cython.org) is a compiler for code written in the
Cython language. Cython is based on Pyrex by Greg Ewing.
...
sage: def has_tty():
....: try:
....: os.open(os.ctermid(), os.O_RDONLY)
....: return True
....: except OSError:
....: return False
sage: (out, err, ret) = test_executable(["sage", "--ecl"], "(* 12345 54321)\n")
sage: out.find("Embeddable Common-Lisp") >= 0
True
sage: out.find("670592745") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--lisp"], "(* 12345 54321)\n")
sage: out.find("Embeddable Common-Lisp") >= 0
True
sage: out.find("670592745") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--gap", "-q"], "Size(SymmetricGroup(5));\n")
sage: out
'120\n'
sage: err.replace('gap: halving pool size.', '').strip()
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--gdb"], 'quit\n') # optional - gdb
sage: out.find('(gdb) ') >= 0 # optional - gdb
True
sage: ret # optional - gdb
0
sage: (out, err, ret) = test_executable(["sage", "--kash", "-b", "3^33;\n"]) # optional - kash
sage: out.find("5559060566555523") >= 0 # optional - kash
True
sage: err # optional - kash
''
sage: ret # optional - kash
0
sage: (out, err, ret) = test_executable(["sage", "--mwrank", "-v0", "-q", "-o"], "0 0 1 -7 6 0 0 0 0 0\n")
sage: out
'Curve [0,0,1,-7,6] :\tRank = 3\n[[3],[[1,-1],[-2,3],[-7/4,25/8]]]\n\n\n'
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--singular"], "12345*54321;\n")
sage: out.find("A Computer Algebra System for Polynomial Computations") >= 0
True
sage: out.find("670592745") >= 0
True
sage: err
''
sage: ret
0
Test GP using the ``-f`` option which prevents the reading of a ``.gprc``
configuration file::
sage: (out, err, ret) = test_executable(["sage", "--gp", "-f"], "3^33\nquit(42)\n")
sage: out.find("PARI/GP") >= 0
True
sage: out.find("5559060566555523") >= 0
True
sage: err
''
sage: ret
42
Some programs of which we check functionality using only ``--version``::
sage: (out, err, ret) = test_executable(["sage", "--maxima", "--version"])
sage: out.find("Maxima ") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--R", "--version"])
sage: out.find("R version ") >= 0
True
sage: err
''
sage: ret
0
sage: (out, err, ret) = test_executable(["sage", "--sqlite3", "--version"])
sage: out.startswith("3.")
True
sage: err
''
sage: ret
0
Check an illegal command line option. This outputs an error to stdout,
but we allow stderr in case this changes in the future::
sage: (out, err, ret) = test_executable(["sage", "--zzfoobar"])
sage: (out+err).find("unknown option: --zzfoobar") >= 0
True
sage: ret > 0
True
Test ``sage --rst2txt file.rst`` on a ReST file::
sage: s = "::\n\n sage: 2^10\n 1024\n sage: 2 + 2\n 4"
sage: input = tmp_filename(ext='.rst')
sage: F = open(input, 'w')
sage: F.write(s)
sage: F.close()
sage: (out, err, ret) = test_executable(["sage", "--rst2txt", input])
sage: print(out)
{{{id=0|
2^10
///
1024
}}}
<BLANKLINE>
{{{id=1|
2 + 2
///
4
}}}
sage: err
''
sage: ret
0
Test ``sage --rst2txt file.rst file.txt`` on a ReST file::
sage: s = "::\n\n sage: 2^10\n 1024\n sage: 2 + 2\n 4"
sage: input = tmp_filename(ext='.rst')
sage: output = tmp_filename(ext='.txt')
sage: F = open(input, 'w')
sage: F.write(s)
sage: F.close()
sage: test_executable(["sage", "--rst2txt", input, output])
('', '', 0)
sage: print(open(output, 'r').read())
{{{id=0|
2^10
///
1024
}}}
<BLANKLINE>
{{{id=1|
2 + 2
///
4
}}}
Test ``sage --rst2sws file.rst file.sws`` on a ReST file::
sage: s = "Thetitle\n--------\n\n::\n\n sage: 2^10\n 1024\n sage: 2 + 2\n 4"
sage: input = tmp_filename(ext='.rst')
sage: output = tmp_filename(ext='.sws')
sage: F = open(input, 'w')
sage: F.write(s)
sage: F.close()
sage: test_executable(["sage", "--rst2sws", input, output])
('', '', 0)
sage: import tarfile
sage: f = tarfile.open(output, 'r')
sage: print(f.extractfile('sage_worksheet/worksheet.html').read())
<h1 class="title">Thetitle</h1>
<BLANKLINE>
{{{id=0|
2^10
///
1024
}}}
<BLANKLINE>
{{{id=1|
2 + 2
///
4
}}}
sage: print(f.extractfile('sage_worksheet/worksheet.txt').read())
Thetitle
system:sage
<BLANKLINE>
<BLANKLINE>
<h1 class="title">Thetitle</h1>
<BLANKLINE>
{{{id=0|
2^10
///
1024
}}}
<BLANKLINE>
{{{id=1|
2 + 2
///
4
}}}
"""
pexpect_env = dict(os.environ)
try:
del pexpect_env["TERM"]
except KeyError:
pass
p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=pexpect_env, **kwds)
if input:
p.stdin.write(input)
p.stdin.close()
fdout = p.stdout.fileno()
fderr = p.stderr.fileno()
out = ""
err = ""
while True:
# Try reading from fdout and fderr
rfd = []
if fdout:
rfd.append(fdout)
if fderr:
rfd.append(fderr)
if len(rfd) == 0:
break
rlist = select.select(rfd, [], [], timeout)[0]
if len(rlist) == 0:
# Timeout!
p.terminate()
raise RuntimeError("timeout in test_executable()")
if fdout in rlist:
s = os.read(fdout, 1024)
if s == "":
fdout = None # EOF
out += s
if fderr in rlist:
s = os.read(fderr, 1024)
if s == "":
fderr = None # EOF
err += s
return (out, err, p.wait())
|