-
Notifications
You must be signed in to change notification settings - Fork 81
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
Re-architecting of CSV / MD / JSON writing #125
Changes from 15 commits
a44ab68
48d4d03
fccfe5a
da00e9b
f187fa1
3ccc408
a4578ec
6b4078b
760dc18
905b91f
1e7f52e
3225035
f99e244
e852e81
bf31668
537f412
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
import sys | ||
import tempfile | ||
import unittest | ||
|
||
from pshtt.models import Domain, Endpoint | ||
from pshtt import pshtt as _pshtt | ||
from pshtt.cli import to_csv | ||
|
||
|
||
class FakeSuffixList(object): | ||
def get_public_suffix(self, hostname, *args, **kwargs): | ||
return hostname | ||
|
||
|
||
# Artificially setup the the preload and suffix lists | ||
# This should be irrelevant after #126 is decided upon / merged | ||
_pshtt.suffix_list = FakeSuffixList() | ||
_pshtt.preload_list = [] | ||
_pshtt.preload_pending = [] | ||
|
||
|
||
class TestToCSV(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
base_domain = 'example.com' | ||
|
||
domain = Domain(base_domain) | ||
domain.http = Endpoint("http", "root", base_domain) | ||
domain.httpwww = Endpoint("http", "www", base_domain) | ||
domain.https = Endpoint("https", "root", base_domain) | ||
domain.httpswww = Endpoint("https", "www", base_domain) | ||
|
||
cls.results = _pshtt.result_for(domain) | ||
cls.temp_filename = os.path.join(tempfile.gettempdir(), 'results.csv') | ||
|
||
@unittest.skipIf(sys.version_info[0] < 3, 'Python 3 test only') | ||
def test_no_results(self): | ||
to_csv([], self.temp_filename) | ||
|
||
with open(self.temp_filename) as fh: | ||
content = fh.read() | ||
|
||
expected = 'Domain,Base Domain,Canonical URL,Live,Redirect,Redirect To,Valid HTTPS,Defaults to HTTPS,Downgrades HTTPS,Strictly Forces HTTPS,HTTPS Bad Chain,HTTPS Bad Hostname,HTTPS Expired Cert,HTTPS Self Signed Cert,HSTS,HSTS Header,HSTS Max Age,HSTS Entire Domain,HSTS Preload Ready,HSTS Preload Pending,HSTS Preloaded,Base Domain HSTS Preloaded,Domain Supports HTTPS,Domain Enforces HTTPS,Domain Uses Strong HSTS,Unknown Error\n' | ||
|
||
self.assertEqual(content, expected) | ||
|
||
@unittest.skipIf(sys.version_info[0] < 3, 'Python 3 test only') | ||
def test_single_result(self): | ||
to_csv([self.results], self.temp_filename) | ||
|
||
with open(self.temp_filename) as fh: | ||
content = fh.read() | ||
|
||
expected = '' | ||
expected += 'Domain,Base Domain,Canonical URL,Live,Redirect,Redirect To,Valid HTTPS,Defaults to HTTPS,Downgrades HTTPS,Strictly Forces HTTPS,HTTPS Bad Chain,HTTPS Bad Hostname,HTTPS Expired Cert,HTTPS Self Signed Cert,HSTS,HSTS Header,HSTS Max Age,HSTS Entire Domain,HSTS Preload Ready,HSTS Preload Pending,HSTS Preloaded,Base Domain HSTS Preloaded,Domain Supports HTTPS,Domain Enforces HTTPS,Domain Uses Strong HSTS,Unknown Error\n' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note as above. |
||
expected += 'example.com,example.com,http://example.com,False,False,,False,False,False,False,False,False,False,False,False,,,False,False,False,False,False,False,False,False,False\n' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be best managed in the test data as an array which is also joined by a comma in code before appending to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Replying to all three comments) Yeah, I actually started down that route originally. For the headers, as you say, using Maybe creating a full domain object and setting all the values there would be best... Let me give that a shot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is true, but it's probably an acceptable annoyance, IMO. I think it's likely to be less brittle than a concatenated string, and so just turning that into a static array would be enough for me for this PR. |
||
|
||
self.assertEqual(content, expected) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
import sys | ||
import tempfile | ||
import unittest | ||
|
||
from pshtt.utils import smart_open | ||
|
||
|
||
class TestSmartOpen(unittest.TestCase): | ||
def test_without_filename(self): | ||
with smart_open() as fh: | ||
self.assertIs(fh, sys.stdout) | ||
|
||
@unittest.skipIf(sys.version_info[0] < 3, 'Python 3 version of test') | ||
def test_with_empty_filename(self): | ||
"""Should raise a `FileNotFoundError`""" | ||
with self.assertRaises(FileNotFoundError): # noqa | ||
with smart_open(''): | ||
pass | ||
|
||
@unittest.skipIf(sys.version_info[0] >= 3, 'Python 2 version of test') | ||
def test_with_empty_filename_python2(self): | ||
"""Should raise a `FileNotFoundError`""" | ||
with self.assertRaises(IOError): | ||
with smart_open(''): | ||
pass | ||
|
||
@unittest.skipIf(sys.version_info[0] < 3, 'Python 3 version of test') | ||
def test_with_real_filename(self): | ||
test_data = 'This is the test data' | ||
|
||
with tempfile.TemporaryDirectory() as tmp_dirname: | ||
# Make a temporary file to use | ||
filename = os.path.join(tmp_dirname, 'foo') | ||
|
||
with smart_open(filename) as fh: | ||
fh.write(test_data) | ||
|
||
self.assertEqual(test_data, open(filename).read()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we generate this by pulling in the global var and joining them with .? That would prevent us from having to update the test data every time a column header changes.