Skip to content

Commit 062e662

Browse files
Added code to write to STDERR when no results found (#22)
* Added code to write to STDERR when no results found * INTEG-935 added changelog
1 parent 80d7a8a commit 062e662

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
99
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.
1010

11+
## 0.4.4 - 2020-04-01
12+
13+
###Added
14+
15+
- Added message to STDERR when no results are found
16+
1117
###Fixed
1218

1319
- Add milliseconds to end timestamp, to represent end of day with milliseconds precision.
1420

21+
## Unreleased
22+
1523
## 0.4.3 - 2020-03-17
1624

1725
### Added

src/code42cli/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.3"
1+
__version__ = "0.4.4"

src/code42cli/securitydata/extraction.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
from code42cli.securitydata.cursor_store import FileEventCursorStore
1616
from code42cli.securitydata.logger_factory import get_error_logger
1717
from code42cli.securitydata.options import ExposureType as ExposureTypeOptions
18-
from code42cli.util import print_error, print_bold, is_interactive
18+
from code42cli.util import print_error, print_bold, is_interactive, print_to_stderr
1919

2020
_EXCEPTIONS_OCCURRED = False
21+
_TOTAL_EVENTS = 0
2122

2223

2324
def extract(output_logger, args):
@@ -142,6 +143,8 @@ def handle_error(exception):
142143
def handle_response(response):
143144
response_dict = json.loads(response.text)
144145
events = response_dict.get(u"fileEvents")
146+
global _TOTAL_EVENTS
147+
_TOTAL_EVENTS += len(events)
145148
for event in events:
146149
output_logger.info(event)
147150

@@ -170,6 +173,9 @@ def _verify_compatibility_with_advanced_query(key, val):
170173
def _handle_result():
171174
if is_interactive() and _EXCEPTIONS_OCCURRED:
172175
print_error(u"View exceptions that occurred at [HOME]/.code42cli/log/code42_errors.")
176+
global _TOTAL_EVENTS
177+
if not _TOTAL_EVENTS:
178+
print_to_stderr(u"No results found\n")
173179

174180

175181
def _try_append_exposure_types_filter(filters, args):

src/code42cli/util.py

+4
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ def get_url_parts(url_str):
7070
if len(parts) > 1 and parts[1] != u"":
7171
port = int(parts[1])
7272
return parts[0], port
73+
74+
75+
def print_to_stderr(error_text):
76+
sys.stderr.write(error_text)

tests/securitydata/test_extraction.py

+18
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,21 @@ def sdk_side_effect(self, *args):
511511

512512
extraction_module.extract(logger, namespace_with_begin)
513513
assert extraction_module._EXCEPTIONS_OCCURRED
514+
515+
516+
def test_extract_when_no_results_are_found_prints_error_to_stderr(
517+
mocker, logger, namespace_with_begin, extractor
518+
):
519+
mock_error = mocker.patch("code42cli.securitydata.extraction.print_to_stderr")
520+
extraction_module._TOTAL_EVENTS = 0
521+
extraction_module.extract(logger, namespace_with_begin)
522+
assert mock_error.call_count
523+
524+
525+
def test_extract_when_results_are_found_does_not_print_error_to_stderr(
526+
mocker, logger, namespace_with_begin, extractor
527+
):
528+
mock_error = mocker.patch("code42cli.securitydata.extraction.print_to_stderr")
529+
extraction_module._TOTAL_EVENTS = 1
530+
extraction_module.extract(logger, namespace_with_begin)
531+
assert not mock_error.call_count

0 commit comments

Comments
 (0)