Skip to content

Commit 05e88d2

Browse files
committed
fix test_cli - change script path, do not import .version if __init__.py is run as a script
1 parent 4e2eeb1 commit 05e88d2

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

tabulate/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def _is_file(f):
2222

2323

2424
__all__ = ["tabulate", "tabulate_formats", "simple_separated_format"]
25-
from .version import version as __version__ # noqa: F401
25+
if __name__ != "__main__":
26+
from .version import version as __version__ # noqa: F401
2627

2728

2829
# minimum extra space in headers

test/test_cli.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
113113

114114
def test_script_from_stdin_to_stdout():
115115
"""Command line utility: read from stdin, print to stdout"""
116-
cmd = [sys.executable, "tabulate.py"]
116+
cmd = [sys.executable, "tabulate/__init__.py"]
117117
out = run_and_capture_stdout(cmd, input=sample_input())
118118
expected = SAMPLE_SIMPLE_FORMAT
119119
print("got: ", repr(out))
@@ -126,7 +126,7 @@ def test_script_from_file_to_stdout():
126126
with TemporaryTextFile() as tmpfile:
127127
tmpfile.write(sample_input())
128128
tmpfile.seek(0)
129-
cmd = [sys.executable, "tabulate.py", tmpfile.name]
129+
cmd = [sys.executable, "tabulate/__init__.py", tmpfile.name]
130130
out = run_and_capture_stdout(cmd)
131131
expected = SAMPLE_SIMPLE_FORMAT
132132
print("got: ", repr(out))
@@ -142,7 +142,7 @@ def test_script_from_file_to_file():
142142
input_file.seek(0)
143143
cmd = [
144144
sys.executable,
145-
"tabulate.py",
145+
"tabulate/__init__.py",
146146
"-o",
147147
output_file.name,
148148
input_file.name,
@@ -165,7 +165,7 @@ def test_script_from_file_to_file():
165165
def test_script_header_option():
166166
"""Command line utility: -1, --header option"""
167167
for option in ["-1", "--header"]:
168-
cmd = [sys.executable, "tabulate.py", option]
168+
cmd = [sys.executable, "tabulate/__init__.py", option]
169169
raw_table = sample_input(with_headers=True)
170170
out = run_and_capture_stdout(cmd, input=raw_table)
171171
expected = SAMPLE_SIMPLE_FORMAT_WITH_HEADERS
@@ -178,7 +178,7 @@ def test_script_header_option():
178178
def test_script_sep_option():
179179
"""Command line utility: -s, --sep option"""
180180
for option in ["-s", "--sep"]:
181-
cmd = [sys.executable, "tabulate.py", option, ","]
181+
cmd = [sys.executable, "tabulate/__init__.py", option, ","]
182182
raw_table = sample_input(sep=",")
183183
out = run_and_capture_stdout(cmd, input=raw_table)
184184
expected = SAMPLE_SIMPLE_FORMAT
@@ -190,7 +190,14 @@ def test_script_sep_option():
190190
def test_script_floatfmt_option():
191191
"""Command line utility: -F, --float option"""
192192
for option in ["-F", "--float"]:
193-
cmd = [sys.executable, "tabulate.py", option, ".1e", "--format", "grid"]
193+
cmd = [
194+
sys.executable,
195+
"tabulate/__init__.py",
196+
option,
197+
".1e",
198+
"--format",
199+
"grid",
200+
]
194201
raw_table = sample_input()
195202
out = run_and_capture_stdout(cmd, input=raw_table)
196203
expected = SAMPLE_GRID_FORMAT_WITH_DOT1E_FLOATS
@@ -202,7 +209,7 @@ def test_script_floatfmt_option():
202209
def test_script_format_option():
203210
"""Command line utility: -f, --format option"""
204211
for option in ["-f", "--format"]:
205-
cmd = [sys.executable, "tabulate.py", "-1", option, "grid"]
212+
cmd = [sys.executable, "tabulate/__init__.py", "-1", option, "grid"]
206213
raw_table = sample_input(with_headers=True)
207214
out = run_and_capture_stdout(cmd, input=raw_table)
208215
expected = SAMPLE_GRID_FORMAT_WITH_HEADERS

0 commit comments

Comments
 (0)