def test_execute_shouldFailAndPrintIfSnippetDoesNotExist(printMock): fileContainsSnippetRule = FileContainsSnippetRule({}, testFolder, fileName, notExistingFileName) assert fileContainsSnippetRule.execute(outputFolder) == False printMock.assert_called_once_with( "assertion fileContainsSnippet {0} {1} failed. {1} does not exist in {2}." .format(fileName, notExistingFileName, testFolder))
def test_execute_shouldLookForLineInFileAndReturnFalseIfItDoesNotExist( printMock): fileContainsSnippetRule = FileContainsSnippetRule({}, testFolder, fileName, badSnippetFile) assert fileContainsSnippetRule.execute(outputFolder) == False printMock.assert_called_once_with( "assertion fileContainsSnippet {0} {1} failed. Matching lines from {1} not found in {2}/{0}." .format(fileName, badSnippetFile, outputFolder))
def test_execute_shouldFailAndPrintIfFileDoesNotExist_case_sensitive( printMock): fileContainsSnippetRule = FileContainsSnippetRule({}, testFolder, fileName.upper(), goodSnippetFile) assert fileContainsSnippetRule.execute(outputFolder) == False printMock.assert_called_once_with( "assertion fileContainsSnippet {0} {1} failed. {0} does not exist in {2}." .format(fileName.upper(), goodSnippetFile, outputFolder))
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
def test_execute_shouldLookForLineInFileAndReturnTrueIfItExists(): fileContainsSnippetRule = FileContainsSnippetRule({}, testFolder, fileName, goodSnippetFile) assert fileContainsSnippetRule.execute(outputFolder)