mirror of
https://github.com/scito/extract_otp_secret_keys.git
synced 2025-12-06 14:54:57 +01:00
add output tests
This commit is contained in:
parent
eac342c0cc
commit
9d08ebaa38
5 changed files with 255 additions and 7 deletions
32
utils.py
32
utils.py
|
|
@ -16,6 +16,27 @@
|
|||
import csv
|
||||
import json
|
||||
import os
|
||||
from io import StringIO
|
||||
import sys
|
||||
|
||||
|
||||
# Ref. https://stackoverflow.com/a/16571630
|
||||
class Capturing(list):
|
||||
'''Capture stdout and stderr
|
||||
Usage:
|
||||
with Capturing() as output:
|
||||
print("Output")
|
||||
'''
|
||||
def __enter__(self):
|
||||
self._stdout = sys.stdout
|
||||
sys.stdout = self._stringio = StringIO()
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.extend(self._stringio.getvalue().splitlines())
|
||||
del self._stringio # free up some memory
|
||||
sys.stdout = self._stdout
|
||||
|
||||
|
||||
def remove_file(filename):
|
||||
if os.path.exists(filename): os.remove(filename)
|
||||
|
|
@ -35,3 +56,14 @@ def read_json(filename):
|
|||
"""Returns a list or a dictionary."""
|
||||
with open(filename, "r") as infile:
|
||||
return json.load(infile)
|
||||
|
||||
|
||||
def read_file_to_list(filename):
|
||||
"""Returns a list of lines."""
|
||||
with open(filename, "r") as infile:
|
||||
return infile.readlines()
|
||||
|
||||
|
||||
def read_file_to_str(filename):
|
||||
"""Returns a str."""
|
||||
return "".join(read_file_to_list(filename))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue