| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Tests for shellapp module. |
| 3 |
|
| 4 |
Authors |
| 5 |
------- |
| 6 |
* Bradley Froehle |
| 7 |
""" |
| 8 |
# ----------------------------------------------------------------------------- |
| 9 |
# Copyright (C) 2012 The IPython Development Team |
| 10 |
# |
| 11 |
# Distributed under the terms of the BSD License. The full license is in |
| 12 |
# the file COPYING, distributed as part of this software. |
| 13 |
# ----------------------------------------------------------------------------- |
| 14 |
|
| 15 |
# ----------------------------------------------------------------------------- |
| 16 |
# Imports |
| 17 |
# ----------------------------------------------------------------------------- |
| 18 |
import unittest |
| 19 |
|
| 20 |
from IPython.testing import decorators as dec |
| 21 |
from IPython.testing import tools as tt |
| 22 |
|
| 23 |
|
| 24 |
class TestFileToRun(tt.TempFileMixin, unittest.TestCase): |
| 25 |
"""Test the behavior of the file_to_run parameter.""" |
| 26 |
|
| 27 |
def test_py_script_file_attribute(self): |
| 28 |
"""Test that `__file__` is set when running `ipython file.py`""" |
| 29 |
src = "print(__file__)\n" |
| 30 |
self.mktmp(src) |
| 31 |
|
| 32 |
err = None |
| 33 |
tt.ipexec_validate(self.fname, self.fname, err) |
| 34 |
|
| 35 |
def test_ipy_script_file_attribute(self): |
| 36 |
"""Test that `__file__` is set when running `ipython file.ipy`""" |
| 37 |
src = "print(__file__)\n" |
| 38 |
self.mktmp(src, ext=".ipy") |
| 39 |
|
| 40 |
err = None |
| 41 |
tt.ipexec_validate(self.fname, self.fname, err) |
| 42 |
|
| 43 |
# The commands option to ipexec_validate doesn't work on Windows, and it |
| 44 |
# doesn't seem worth fixing |
| 45 |
@dec.skip_win32 |
| 46 |
def test_py_script_file_attribute_interactively(self): |
| 47 |
"""Test that `__file__` is not set after `ipython -i file.py`""" |
| 48 |
src = "True\n" |
| 49 |
self.mktmp(src) |
| 50 |
|
| 51 |
out, err = tt.ipexec( |
| 52 |
self.fname, |
| 53 |
options=["-i"], |
| 54 |
commands=['"__file__" in globals()', "print(123)", "exit()"], |
| 55 |
) |
| 56 |
assert "False" in out, f"Subprocess stderr:\n{err}\n-----" |