-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add tests for a variety of util functions #2496
Conversation
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.
Thanks for adding tests!
return None # Resource cannot be resolved | ||
|
||
|
||
def read_stripped_lines(filename): |
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.
This seems like an opportunity to optimize this to use a generator.
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.
Would that be considered a non-breaking change? You can't index a generator...but it's quite a small break so maybe it's ok...
At the very least I can add a todo comment
Returns: | ||
True if internet connection can be detected | ||
""" | ||
if _connected_dns(): |
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.
It seems to me like the dns check is sufficient, and all the checks are potentially DoS and information disclosure concerns.
See here for an example of this type of problem in the wild.
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.
The checks have grown a bit over time. The dns lookup doesn't work on all networks due to filtering, hence ncis check...
The google check was added to handle some weird edge cases filtering out Mark-1's where the AP wasn't shut off completely. It should be removed at some point
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.
yeah, I guess I'm just not fully understanding why we'd use both domains here. Outbound port filtering makes sense for DNS vs HTTP protocols, I guess we're trying another tactic with http/https ports?
A "good internet citizen" would probably try to connect to a mycroft-operated or public for-this-purpose endpoint. NCSI seems to hit that (if intended for msft users), but dns and google.com don't strike me as good choices here.
|
||
|
||
@mock.patch('mycroft.configuration.Configuration') | ||
class TestTimeFuncs(TestCase): |
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.
future improvement: A more common strategy to testing time is to introduce the concept of a Clock as an interface, and provide deterministic clocks at test time. This prevents the necessity of mocking global statics, which can cause cascading failures through tests (and introduce bugs!) when misused.
Updated marking the redundant string functions as to be removed and changed the read_stripped_lines to a generator as suggested. |
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.
If we are going to take the time to refactor some of this code, let's get after it!
test/unittests/util/test_util.py
Outdated
from unittest import TestCase, mock | ||
|
||
from mycroft import MYCROFT_ROOT_PATH | ||
from mycroft.util import (resolve_resource_file, curate_cache, |
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.
convert to new directory structure (e.g. from mycroft.util.audio_util
, etc.). should these tests be broken up into one file per module being tested?
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.
mycroft.util
should still export all these methods so I think it's better for the test to pull from the module and not the submodules
I didn't originally intend to do too much changes to the functions themselves, just organize them a bit better but I'll continue the refactoring and clean up the code as much as I get time to this weekend. |
Understood, but this is how we make the code better, by chipping away at it like this. If we don't make these changes as we touch the code, when do we? |
The next time? I have this as an ongoing weekend morning project, chipping away pushing PR's at logical intervals. However now I've almost completed the second pass of this so I can just as well add ot to this PR even if it makes the reviewing much more involved |
165fbbe
to
4582597
Compare
3795f0a
to
5a6d4b6
Compare
Voight Kampff Integration Test Succeeded (Results) |
Comment says the folder to be checked should be datadir/res, this makes the code mean the same thing.
Move utils away from the __init__.py file
- create helper functions for getting file stats and removing files in order of age - create wrapper function for turning MB into bytes
- Remove superfluous parentheses - Remove unneccessary pass
ensure_directory_exists() now takes the directory permissions as an optional argument.
Adds comments for PULSE ENVIRONMENT
Voight Kampff Integration Test Succeeded (Results) |
Voight Kampff Integration Test Succeeded (Results) |
I did a final update to the Logging so the known error (FileNotFoundError) will log a short informative message while an unknown error (Exception) will show the stack trace. |
Description
__init__.py
into subfiles based on functionality.This now changes the read_stripped_lines to a generator. This changes the return type and may be considered breaking but it'll rarely be used indexed and there's currently nothing that's using the function as far as I've been able to determine.
How to test
Ensure that mycroft is still functioning and that tests passes.
Contributor license agreement signed?
CLA [ Yes ]