Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Add tests for a variety of util functions #2496

Merged
merged 26 commits into from
Apr 27, 2020
Merged

Conversation

forslund
Copy link
Collaborator

@forslund forslund commented Mar 9, 2020

Description

  • Add test cases
  • Logic change in resolve_resource_file to make it work as the docstring indicated.
  • split the __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 ]

@devs-mycroft devs-mycroft added the CLA: Yes Contributor License Agreement exists (see https://github.com/MycroftAI/contributors) label Mar 9, 2020
Copy link
Contributor

@clusterfudge clusterfudge left a 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):
Copy link
Contributor

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.

Copy link
Collaborator Author

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():
Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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.

mycroft/util/string_utils.py Show resolved Hide resolved


@mock.patch('mycroft.configuration.Configuration')
class TestTimeFuncs(TestCase):
Copy link
Contributor

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.

@forslund
Copy link
Collaborator Author

Updated marking the redundant string functions as to be removed and changed the read_stripped_lines to a generator as suggested.

@forslund forslund added Type: Refactoring and other improvements Improvement of code and documentation that does not alter functionality. type: test labels Mar 26, 2020
Copy link
Member

@chrisveilleux chrisveilleux left a 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!

mycroft/util/audio_utils.py Show resolved Hide resolved
mycroft/util/audio_utils.py Outdated Show resolved Hide resolved
mycroft/util/audio_utils.py Outdated Show resolved Hide resolved
mycroft/util/audio_utils.py Show resolved Hide resolved
mycroft/util/audio_utils.py Show resolved Hide resolved
mycroft/util/file_utils.py Outdated Show resolved Hide resolved
mycroft/util/file_utils.py Outdated Show resolved Hide resolved
mycroft/util/process_utils.py Show resolved Hide resolved
test/unittests/util/test_time.py Outdated Show resolved Hide resolved
from unittest import TestCase, mock

from mycroft import MYCROFT_ROOT_PATH
from mycroft.util import (resolve_resource_file, curate_cache,
Copy link
Member

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?

Copy link
Collaborator Author

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

@forslund
Copy link
Collaborator Author

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.

@chrisveilleux
Copy link
Member

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?

@forslund
Copy link
Collaborator Author

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

@forslund forslund force-pushed the test/util branch 2 times, most recently from 165fbbe to 4582597 Compare March 29, 2020 13:46
@forslund forslund force-pushed the test/util branch 3 times, most recently from 3795f0a to 5a6d4b6 Compare April 11, 2020 06:20
@devops-mycroft
Copy link

Voight Kampff Integration Test Succeeded (Results)

@devops-mycroft
Copy link

Voight Kampff Integration Test Succeeded (Results)

@MycroftAI MycroftAI deleted a comment from devops-mycroft Apr 18, 2020
@devops-mycroft
Copy link

Voight Kampff Integration Test Succeeded (Results)

@forslund
Copy link
Collaborator Author

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.

@forslund forslund merged commit bcb426a into MycroftAI:dev Apr 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA: Yes Contributor License Agreement exists (see https://github.com/MycroftAI/contributors) Type: Refactoring and other improvements Improvement of code and documentation that does not alter functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants