Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdin: fix false no file test and add tests for stdin #271

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pip install xlsx2csv
```
**positional arguments:**
```
xlsxfile xlsx file path
xlsxfile xlsx file path, use '-' to read from STDIN
outfile output csv file path, or directory if -s 0 is specified
```
**optional arguments:**
Expand Down
4 changes: 4 additions & 0 deletions man/xlsx2csv.1.pod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ xlsx2csv - Convert xlsx xml files to csv format

The conversion uses Expat SAX parser for xml processing.

=head1 STDIN

Use "-" for INFILE to read from the STDIN.

=head1 OPTIONS

=over 4
Expand Down
26 changes: 23 additions & 3 deletions test/run
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def compare(case, arguments=[]):
ext = "xlsx"
if os.path.exists("test/%s.xlsm" % case):
ext = "xlsm"

if os.name == 'posix':# in case of Linux
left = subprocess.check_output(["python%s" %pyver, "./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','')
command = ["python%s" %pyver]
elif os.name == 'nt':# in case of Windows
# Use py.exe http://blog.python.org/2011/07/python-launcher-for-windows_11.html on Windows
left = subprocess.check_output(["py", "-%s" %pyver, "./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','')
command = ["py", "-%s" %pyver]
else:
print("os.name is unexpected: "+os.name)
sys.exit(1)
left = subprocess.check_output(command + ["./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','')

f = open("test/%s.csv" %case, "r", encoding="utf-8", newline="")
right = f.read().replace('\r','')
Expand All @@ -41,9 +42,28 @@ def compare(case, arguments=[]):
failed = True
else:
print("OK: %s %s" %(case, pyver))

# test STDIN, only works for python3
if pyver == "2":
continue

xfile = open("test/%s.%s" %(case,ext), "rb")
stdin = xfile.read()
xfile.close()

pipe = subprocess.run(command + ["./xlsx2csv.py"] + arguments + ["-"], input = stdin, capture_output = True)
stdinleft = pipe.stdout.decode("utf-8").replace('\r','').replace('\r','')

if stdinleft != right:
print("FAILED (STDIN): %s %s" %(case, pyver))
failed = True
else:
print("OK (STDIN): %s %s" %(case, pyver))

if failed:
sys.exit(1)


compare("datetime", ["--dateformat=%Y-%m-%d %H:%M:%S"])
compare("empty_row")
compare("junk-small")
Expand Down
4 changes: 2 additions & 2 deletions xlsx2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def main():

if "ArgumentParser" in globals():
parser = ArgumentParser(description="xlsx to csv converter")
parser.add_argument('infile', metavar='xlsxfile', help="xlsx file path")
parser.add_argument('infile', metavar='xlsxfile', help="xlsx file path, use '-' to read from STDIN")
parser.add_argument('outfile', metavar='outfile', nargs='?', help="output csv file path")
parser.add_argument('-v', '--version', action='version', version=__version__)
nargs_plus = "+"
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def main():
try:
if os.path.isdir(options.infile):
convert_recursive(options.infile, sheetid, outfile, kwargs)
elif not os.path.exists(options.infile):
elif not os.path.exists(options.infile) and options.infile != "-":
raise InvalidXlsxFileException("Input file not found!")
else:
xlsx2csv = Xlsx2csv(options.infile, **kwargs)
Expand Down