diff --git a/rflint/rflint.py b/rflint/rflint.py index 8355fd9..668c0ab 100644 --- a/rflint/rflint.py +++ b/rflint/rflint.py @@ -102,6 +102,13 @@ def run(self, args): self.counts = { ERROR: 0, WARNING: 0, "other": 0} + self.filetypes = [f".{ft}" for ft in self.args.filetypes.split(',')] + supported_file_types = [".robot", ".resource", ".tsv", ".txt", ".rst"] + for ft in self.filetypes: + if ft not in supported_file_types: + sys.stderr.write(f"rflint: File extension {ft} is not supported") + return -1 + for filename in self.args.args: if not (os.path.exists(filename)): sys.stderr.write("rflint: %s: No such file or directory\n" % filename) @@ -109,7 +116,9 @@ def run(self, args): if os.path.isdir(filename): self._process_folder(filename) else: - self._process_file(filename) + _, ext = os.path.splitext(filename) + if ext.lower() in self.filetypes: + self._process_file(filename) if self.counts[ERROR] > 0: return self.counts[ERROR] if self.counts[ERROR] < 254 else 255 @@ -143,8 +152,8 @@ def _process_folder(self, path): def _process_files(self, folder, filenames): for filename in filenames: - name, ext = os.path.splitext(filename) - if ext.lower() in (".robot", ".txt", ".tsv", ".resource"): + _, ext = os.path.splitext(filename) + if ext.lower() in self.filetypes: self._process_file(os.path.join(folder, filename)) def _process_file(self, filename): @@ -266,6 +275,12 @@ def parse_and_process_args(self, args): "\n" "If you give a directory as an argument, all files in the directory\n" "with the suffix .txt, .robot, .resource, or .tsv will be processed. \n" + "You can explicitly provide file types which you want to run\n" + "with option --filetypes or -t. Defaults are robot and resource.\n" + "\n" + "For example: '--filetypes robot,resource' will ignore all files\n" + "with extensions other than .robot or .resource.\n" + "\n" "With the --recursive option, subfolders within the directory will \n" "also be processed." ) @@ -286,6 +301,9 @@ def parse_and_process_args(self, args): parser.add_argument("--format", "-f", help="Define the output format", default='{severity}: {linenumber}, {char}: {message} ({rulename})') + parser.add_argument("--filetypes", "-t", + help="Select file extensions to scan separated by comma", + default='robot,resource') parser.add_argument("--version", action="store_true", default=False, help="Display version number and exit") parser.add_argument("--verbose", "-v", action="store_true", default=False, diff --git a/test_data/acceptance/filetypes/test.resource b/test_data/acceptance/filetypes/test.resource new file mode 100644 index 0000000..1beb355 --- /dev/null +++ b/test_data/acceptance/filetypes/test.resource @@ -0,0 +1,2 @@ +* Invalid + diff --git a/test_data/acceptance/filetypes/test.robot b/test_data/acceptance/filetypes/test.robot new file mode 100644 index 0000000..17bbc89 --- /dev/null +++ b/test_data/acceptance/filetypes/test.robot @@ -0,0 +1,3 @@ +*** Test Cases *** +Simple Test Case + Pass Execution Everything's fine diff --git a/test_data/acceptance/filetypes/test.tsv b/test_data/acceptance/filetypes/test.tsv new file mode 100644 index 0000000..17bbc89 --- /dev/null +++ b/test_data/acceptance/filetypes/test.tsv @@ -0,0 +1,3 @@ +*** Test Cases *** +Simple Test Case + Pass Execution Everything's fine diff --git a/test_data/acceptance/filetypes/test.txt b/test_data/acceptance/filetypes/test.txt new file mode 100644 index 0000000..17bbc89 --- /dev/null +++ b/test_data/acceptance/filetypes/test.txt @@ -0,0 +1,3 @@ +*** Test Cases *** +Simple Test Case + Pass Execution Everything's fine diff --git a/tests/acceptance/filetypes.robot b/tests/acceptance/filetypes.robot new file mode 100644 index 0000000..fd37a20 --- /dev/null +++ b/tests/acceptance/filetypes.robot @@ -0,0 +1,104 @@ +*** Settings *** +Documentation Check if rflint reads supported file formats +Library OperatingSystem +Library Process +Library String +Resource SharedKeywords.robot + + +*** Test Cases *** +All Supported File Types + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + robot resource tsv + +.robot And .resource Supported File Types + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + robot resource + +.resource And .tsv Supported File Types + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + resource tsv + +.robot And .tsv Supported File Types + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + robot tsv + +Only .robot Supported File Type + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + robot + +Only .resource Supported File Type + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + resource + +Only .tsv Supported File Type + [Template] Run Rflint And Verify There Are No Errors For Supported File Types + tsv + +Only .pdf Unsupported File Type + [Template] Run Rflint And Verify That Unsupported File Types Returned Errors + pdf + +Specific .txt File With .robot Provided File Type + [Template] Run Rflint And Verify There Are No Output For Not Matching File Types + test_data/acceptance/filetypes/test.txt robot + +Specific .robot File With .resource Provided File Type + [Template] Run Rflint And Verify There Are No Output For Not Matching File Types + test_data/acceptance/filetypes/test.robot resource + +Specific .resource File With .robot And .tsv Provided File Type + [Template] Run Rflint And Verify There Are No Output For Not Matching File Types + test_data/acceptance/filetypes/test.resource robot tsv + +Specific .tsv File With .robot And .resource Provided File Type + [Template] Run Rflint And Verify There Are No Output For Not Matching File Types + test_data/acceptance/filetypes/test.tsv robot resource + + +*** Keywords *** +Run Rflint And Verify There Are No Errors For Supported File Types + [Arguments] @{extensions} + ${file_types} Parse File Types @{extensions} + Run rf-lint with the following options: + ... --filetypes ${file_types} + ... test_data/acceptance/filetypes + FOR ${file_type} IN @{extensions} + Should Contain ${result.stdout} test.${file_type} + END + Should Be Empty ${result.stderr} + +Run Rflint And Verify That Unsupported File Types Returned Errors + [Arguments] @{extensions} + ${file_types} Parse File Types @{extensions} + Run rf-lint with the following options: + ... --filetypes ${file_types} + ... test_data/acceptance/filetypes + FOR ${file_type} IN @{extensions} + Should Contain ${result.stderr} rflint: File extension .${file_type} is not supported + END + Should Be Empty ${result.stdout} + +Run Rflint And Verify There Are No Output For Not Matching File Types + [Arguments] ${path_to_file} @{extensions} + ${file_types} Parse File Types @{extensions} + Run rf-lint with the following options: + ... -t ${file_types} + ... ${path_to_file} + FOR ${file_type} IN @{extensions} + Should Be Empty ${result.stdout} ${EMPTY} + Should Be Empty ${result.stderr} ${EMPTY} + END + Should Be Empty ${result.stderr} + +Parse File Types + [Arguments] @{filetypes} + ${types} Set Variable ${EMPTY} + FOR ${index} ${file_type} IN ENUMERATE @{filetypes} + ${types} Run Keyword If ${index}==${0} + ... Set Variable ${file_type} + ... ELSE + ... Set Variable ${types},${file_type} + END + Log Provided filetypes: ${types} + [Return] ${types}