github.com/pkumar631/talisman@v0.3.2/detector/detection_results_test.go (about)

     1  package detector
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestNewDetectionResultsAreSuccessful(t *testing.T) {
    10  	results := NewDetectionResults()
    11  	assert.True(t, results.Successful(), "New detection result is always expected to succeed")
    12  	assert.False(t, results.HasFailures(), "New detection result is not expected to fail")
    13  }
    14  
    15  func TestCallingFailOnDetectionResultsFails(t *testing.T) {
    16  	results := NewDetectionResults()
    17  	results.Fail("some_filename", "Bomb")
    18  	assert.False(t, results.Successful(), "Calling fail on a result should not make it succeed")
    19  	assert.True(t, results.HasFailures(), "Calling fail on a result should make it fail")
    20  }
    21  
    22  func TestCanRecordMultipleErrorsAgainstASingleFile(t *testing.T) {
    23  	results := NewDetectionResults()
    24  	results.Fail("some_filename", "Bomb")
    25  	results.Fail("some_filename", "Complete & utter failure")
    26  	results.Fail("another_filename", "Complete & utter failure")
    27  	assert.Len(t, results.Failures("some_filename"), 2, "Expected two errors against some_filename.")
    28  	assert.Len(t, results.Failures("another_filename"), 1, "Expected one error against another_filename")
    29  }
    30  
    31  func TestResultsReportsFailures(t *testing.T) {
    32  	results := NewDetectionResults()
    33  	results.Fail("some_filename", "Bomb")
    34  	results.Fail("some_filename", "Complete & utter failure")
    35  	results.Fail("another_filename", "Complete & utter failure")
    36  
    37  	actualErrorReport := results.ReportFileFailures("some_filename")
    38  	assert.Regexp(t, "The following errors were detected in some_filename", actualErrorReport, "Error report does not contain expected output")
    39  	assert.Regexp(t, "Bomb", actualErrorReport, "Error report does not contain expected output")
    40  	assert.Regexp(t, "Complete & utter failure", actualErrorReport, "Error report does not contain expected output")
    41  }
    42  
    43  func TestLoggingIgnoredFilesDoesNotCauseFailure(t *testing.T) {
    44  	results := NewDetectionResults()
    45  	results.Ignore("some_file", "Ignoring this file, just because")
    46  	results.Ignore("some/other_file", "Ignoring this file too")
    47  	assert.True(t, results.Successful(), "Calling ignore should keep the result successful.")
    48  	assert.True(t, results.HasIgnores(), "Calling ignore should be logged.")
    49  	assert.False(t, results.HasFailures(), "Calling ignore should not cause a result to fail.")
    50  
    51  	assert.Regexp(t, "Ignoring this file, just because", results.Report(), "foo")
    52  	assert.Regexp(t, "Ignoring this file too", results.Report(), "foo")
    53  }