def test_printDifferencesHandlesBinary(readLinesMock, printMock): readLinesMock.side_effect = UnicodeDecodeError('encoding', bytearray(1), 0, 1, 'test error') fileMatchesRule = FileMatchesRule({}, testFolder, fileName, fixturePath) fileMatchesRule.printDifferences(fileName, fixturePath) printMock.assert_called_once_with( "One or both files are binary, unable to print differences")
def test_execute_shouldReturnTrueIfTheFilesMatch(filecmpMock, pathExistsMock): pathExistsMock.return_value = True filecmpMock.return_value = True fileMatchesRule = FileMatchesRule({}, testFolder, fileName, fixturePath) assert fileMatchesRule.execute(outputFolder) filecmpMock.assert_called_once_with(os.path.join(outputFolder, fileName), os.path.join(testFolder, fixturePath), shallow=False)
def test_execute_shouldReturnFalseAndNotThrowIfFixtureDoesNotExist( printMock, filecmpMock, pathExistsMock): filecmpMock.side_effect = throwFixtureFileNotFound pathExistsMock.side_effect = fixtureFileDoesNotExist fileMatchesRule = FileMatchesRule({}, testFolder, fileName, fixturePath) assert not fileMatchesRule.execute(outputFolder) printMock.assert_called_once_with( "assertion fileMatches " + fileName + " " + fixturePath + " failed. " + os.path.abspath(os.path.join(testFolder, fixturePath)) + " does not exist")
def test_execute_shouldReturnFalseIfTheFilesDoNotMatch(printMock, filecmpMock, pathExistsMock): filecmpMock.return_value = False pathExistsMock.return_value = True fileMatchesRule = FileMatchesRule({}, testFolder, fileName, fixturePath) fileMatchesRule.printDifferences = MagicMock(name='printDifferences') assert not fileMatchesRule.execute(outputFolder) printMock.assert_called_once_with("assertion fileMatches " + fileName + " " + fixturePath + " failed. Files differ")
def test_printDifferencesPrintsFileDiff(echoMock, styleMock, diffMock, readLinesMock): readLinesMock.side_effect = readLinesSideEffect diffLines = ["+ test output line\n", "- test fixture line\n"] diffMock.return_value = diffLines styleMock.side_effect = styleSideEffect fileMatchesRule = FileMatchesRule({}, testFolder, fileName, fixturePath) fileMatchesRule.printDifferences(fileName, fixturePath) diffMock.assert_called_once_with(wrongOutputLines, fixtureFileLines, fromfile=fileName, tofile=fixturePath) echoMock.assert_any_call("ANSI STYLED blue:: " + diffLines[0]) echoMock.assert_any_call("ANSI STYLED yellow:: " + diffLines[1])
def test_printDifferencesPrintsFileDiffWithVisibleWhitepsace( echoMock, styleMock, diffMock, readLinesMock): readLinesMock.side_effect = readLinesSideEffect diffLines = [ "--- integrationTests/visible-spaces/test/spaces/build/MyApp/file-with-spaces\n", "+++ integrationTests/visible-spaces/test/spaces/expected-file-with-spaces\n", "@@ -1,3 +1,3 @@\n", " line with spaces\n", "-foo is bar\n", "-foo is bar\n", "+ foo is bar \n", "+ foo is bar \r\n", " other line\n" ] diffLinesWithVisisbleSpaces = [ "--- integrationTests/visible-spaces/test/spaces/build/MyApp/file-with-spaces\n", "+++ integrationTests/visible-spaces/test/spaces/expected-file-with-spaces\n", "@@ -1,3 +1,3 @@\n", " line•with•spaces¶", "-foo•is•bar¶", "-foo→is→bar¶", "+•foo••is••bar••¶", "+→foo→→is→bar→↵¶", " other•••line¶" ] diffMock.return_value = diffLines styleMock.side_effect = styleSideEffect fileMatchesRule = FileMatchesRule({VISIBLE_WHITESPACE: True}, testFolder, fileName, fixturePath) fileMatchesRule.printDifferences(fileName, fixturePath) diffMock.assert_called_once_with(wrongOutputLines, fixtureFileLines, fromfile=fileName, tofile=fixturePath) echoMock.assert_any_call("ANSI STYLED yellow:: " + diffLinesWithVisisbleSpaces[0]) echoMock.assert_any_call("ANSI STYLED blue:: " + diffLinesWithVisisbleSpaces[1]) echoMock.assert_any_call(diffLinesWithVisisbleSpaces[2]) echoMock.assert_any_call(diffLinesWithVisisbleSpaces[3]) echoMock.assert_any_call("ANSI STYLED yellow:: " + diffLinesWithVisisbleSpaces[4]) echoMock.assert_any_call("ANSI STYLED yellow:: " + diffLinesWithVisisbleSpaces[5]) echoMock.assert_any_call("ANSI STYLED blue:: " + diffLinesWithVisisbleSpaces[6]) echoMock.assert_any_call("ANSI STYLED blue:: " + diffLinesWithVisisbleSpaces[7]) echoMock.assert_any_call(diffLinesWithVisisbleSpaces[8])
def test_printDifferencesPrintsFileDiff(echoMock, styleMock, diffMock, readLinesMock): readLinesMock.side_effect = readLinesSideEffect diffLines = [ "--- integrationTests/visible-spaces/test/spaces/build/MyApp/file-with-spaces\n", "+++ integrationTests/visible-spaces/test/spaces/expected-file-with-spaces\n", "@@ -1,3 +1,3 @@\n", "- test output line\n", "+ test fixture line\n" ] diffMock.return_value = diffLines styleMock.side_effect = styleSideEffect fileMatchesRule = FileMatchesRule({}, testFolder, fileName, fixturePath) fileMatchesRule.printDifferences(fileName, fixturePath) diffMock.assert_called_once_with(wrongOutputLines, fixtureFileLines, fromfile=fileName, tofile=fixturePath) echoMock.assert_any_call("ANSI STYLED yellow:: " + diffLines[0]) echoMock.assert_any_call("ANSI STYLED blue:: " + diffLines[1]) echoMock.assert_any_call(diffLines[2]) echoMock.assert_any_call("ANSI STYLED yellow:: " + diffLines[3][0:-1]) echoMock.assert_any_call("ANSI STYLED blue:: " + diffLines[4][0:-1])
def test_parseAssertionFile_clie_options_should_override_assertion_file(): currentFolder = os.path.dirname(os.path.abspath(__file__)) testFolder = Path(currentFolder).parent.joinpath("example") assertionFile = testFolder.joinpath( "sampleAssertionFileWithVisibleWhitespace.yaml") cli_options = {VISIBLE_WHITESPACE: False} #opposite of what's in yaml file expectedRules = [] expectedRules.append( PathExistsRule(cli_options, str(testFolder), "foo.txt")) expectedRules.append(PathExistsRule(cli_options, str(testFolder), "bin")) expectedRules.append( PathNotExistsRule(cli_options, str(testFolder), "missingFile")) expectedRules.append( FileMatchesRule(cli_options, str(testFolder), "build.gradle", "expectedBuild.gradle")) expectedRules.append( RunScriptRule(cli_options, str(testFolder), "MyApp", "./gradlew clean build")) expectedRules.append( FileContainsLineRule(cli_options, str(testFolder), "MyApp/foo", "this line should exist")) expectedRules.append( FileDoesNotContainLineRule(cli_options, str(testFolder), "MyApp/foo", "this line should not exist")) expectedRules.append( FileHasRegexMatchLineRule(cli_options, str(testFolder), "MyApp/foo", "^lo+king \\sfor.*$")) expectedRules.append( FileDoesNotRegexMatchRule(cli_options, str(testFolder), "MyApp/foo", "^lo+king\\s[fdfgd]{4} or.*$")) expectedRules.append( FileContainsSnippetRule(cli_options, str(testFolder), "MyApp/foo", "goodSnippet.txt")) expectedRules.append( FileDoesNotContainSnippetRule(cli_options, str(testFolder), "MyApp/foo", "badSnippet.txt")) actualRules = assertion_file_parser.parseAssertionFile( str(assertionFile), str(testFolder), cli_options) assert expectedRules == actualRules
def test_parseAssertionFile_shouldReturnAssertionRuleArray(): currentFolder = os.path.dirname(os.path.abspath(__file__)) testFolder = Path(currentFolder).parent.joinpath("example") assertionFile = testFolder.joinpath("sampleAssertionFile.yaml") expectedRules = [] expectedRules.append(PathExistsRule({}, str(testFolder), "foo.txt")) expectedRules.append(PathExistsRule({}, str(testFolder), "bin")) expectedRules.append(PathNotExistsRule({}, str(testFolder), "missingFile")) expectedRules.append( FileMatchesRule({}, str(testFolder), "build.gradle", "expectedBuild.gradle")) expectedRules.append( RunScriptRule({}, str(testFolder), "MyApp", "./gradlew clean build")) expectedRules.append( FileContainsLineRule({}, str(testFolder), "MyApp/foo", "this line should exist")) expectedRules.append( FileDoesNotContainLineRule({}, str(testFolder), "MyApp/foo", "this line should not exist")) expectedRules.append( FileHasRegexMatchLineRule({}, str(testFolder), "MyApp/foo", "^lo+king \\sfor.*$")) expectedRules.append( FileDoesNotRegexMatchRule({}, str(testFolder), "MyApp/foo", "^lo+king\\s[fdfgd]{4} or.*$")) expectedRules.append( FileContainsSnippetRule({}, str(testFolder), "MyApp/foo", "goodSnippet.txt")) expectedRules.append( FileDoesNotContainSnippetRule({}, str(testFolder), "MyApp/foo", "badSnippet.txt")) actualRules = assertion_file_parser.parseAssertionFile( str(assertionFile), str(testFolder), {}) assert expectedRules == actualRules
def parseAssertionFile(assertionFile, testFolder, cli_options): rules = [] with open(assertionFile, 'r') as yamlAssertionFile: assertionData = yaml.load(yamlAssertionFile, Loader=yaml.FullLoader) options = {} if ('options' in assertionData): options = assertionData['options'] options.update(cli_options) for ruleString in assertionData["assertions"]: tokens = ruleString.split() if (tokens[0] == "pathExists"): rule = PathExistsRule(options, testFolder, tokens[1]) rules.append(rule) elif (tokens[0] == "fileMatches"): rule = FileMatchesRule(options, testFolder, tokens[1], tokens[2]) rules.append(rule) elif (tokens[0] == "pathNotExists"): rule = PathNotExistsRule(options, testFolder, tokens[1]) rules.append(rule) elif (tokens[0] == "runScript"): script = getRestOfLineWithSpacesStartingWithToken( 2, tokens, ruleString) rule = RunScriptRule(options, testFolder, tokens[1], script) rules.append(rule) elif (tokens[0] == "fileContainsLine"): line = getRestOfLineWithSpacesStartingWithToken( 2, tokens, ruleString) rule = FileContainsLineRule(options, testFolder, tokens[1], line) rules.append(rule) elif (tokens[0] == "fileDoesNotContainLine"): line = getRestOfLineWithSpacesStartingWithToken( 2, tokens, ruleString) rule = FileDoesNotContainLineRule(options, testFolder, tokens[1], line) rules.append(rule) elif (tokens[0] == "fileHasMatchingLine"): regex = getRestOfLineWithSpacesStartingWithToken( 2, tokens, ruleString) rule = FileHasRegexMatchLineRule(options, testFolder, tokens[1], regex) rules.append(rule) elif (tokens[0] == "fileDoesNotHaveMatchingLine"): regex = getRestOfLineWithSpacesStartingWithToken( 2, tokens, ruleString) rule = FileDoesNotRegexMatchRule(options, testFolder, tokens[1], regex) rules.append(rule) elif (tokens[0] == "fileContainsSnippet"): rule = FileContainsSnippetRule(options, testFolder, tokens[1], tokens[2]) rules.append(rule) elif (tokens[0] == "fileDoesNotContainSnippet"): rule = FileDoesNotContainSnippetRule(options, testFolder, tokens[1], tokens[2]) rules.append(rule) else: messager.printError( "Unable to parse assertion file {}. Error on assertion \"{}\"" .format(assertionFile, ruleString)) return [] return rules