Skip to content

Commit

Permalink
Add unit tests for new precommit hook json feature (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdakran authored Jun 30, 2022
1 parent c48c0a0 commit 2fc0e31
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/pre_commit_hook_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import io
import json
import sys
from contextlib import contextmanager
from functools import partial
from typing import List
Expand Down Expand Up @@ -80,6 +82,44 @@ def test_baseline_filters_out_known_secrets():
])


def test_console_output():
command = ['test_data/files/file_with_secrets.py']

# Redirect stdout
capturedOutput = io.StringIO()
sys.stdout = capturedOutput

main(command)

# Reset redirect stdout
sys.stdout = sys.__stdout__

# Assert formatting
output = capturedOutput.getvalue()
assert output.startswith('ERROR: Potential secrets about to be committed to git repo!')


def test_console_output_json_formatting():
command = ['--json', 'test_data/files/file_with_secrets.py']

# Redirect stdout
capturedOutput = io.StringIO()
sys.stdout = capturedOutput

main(command)

# Reset redirect stdout
sys.stdout = sys.__stdout__

# Assert formatting
data = json.loads(capturedOutput.getvalue())
assert(data['version'])
assert(data['plugins_used'])
assert(data['filters_used'])
assert(data['results'])
assert(data['generated_at'])


class TestModifiesBaselineFromVersionChange:
FILENAME = 'test_data/files/file_with_secrets.py'

Expand Down

0 comments on commit 2fc0e31

Please sign in to comment.