2 new revisions:

Revision: 6ab475b2ea57
Branch:   default
Author:   Pekka Klärck
Date:     Wed Jan 16 03:54:26 2013
Log:      Fixed tidy error handling when given input is not a file....
http://code.google.com/p/robotframework/source/detail?r=6ab475b2ea57

Revision: 7b317123001d
Branch:   default
Author:   Pekka Klärck
Date:     Wed Jan 16 05:35:36 2013
Log: Tidy: Moved argument validation into a separate class and slightly enh...
http://code.google.com/p/robotframework/source/detail?r=7b317123001d

==============================================================================
Revision: 6ab475b2ea57
Branch:   default
Author:   Pekka Klärck
Date:     Wed Jan 16 03:54:26 2013
Log:      Fixed tidy error handling when given input is not a file.

Update issue 1333
Status: Started
Owner: pekka.klarck
Fixed with unit tests. Will refactor code next.
http://code.google.com/p/robotframework/source/detail?r=6ab475b2ea57

Modified:
 /src/robot/tidy.py
 /utest/tidy/test_argument_validation.py

=======================================
--- /src/robot/tidy.py  Mon Jan  7 12:10:29 2013
+++ /src/robot/tidy.py  Wed Jan 16 03:54:26 2013
@@ -199,10 +199,26 @@
def _validate_mode_and_arguments(self, args, inplace, recursive, **others):
         if inplace and recursive:
raise DataError('--recursive and --inplace can not be used together.')
-        if recursive and (len(args) > 1 or not os.path.isdir(args[0])):
+        elif recursive:
+            self._validate_recursive_arguments(args)
+        elif inplace:
+            self._validate_inplace_arguments(args)
+        else:
+            self._validate_default_mode_arguments(args)
+
+    def _validate_recursive_arguments(self, args):
+        if len(args) != 1 or not os.path.isdir(args[0]):
raise DataError('--recursive requires exactly one directory as argument.')
-        if not (inplace or recursive) and len(args) > 2:
+
+    def _validate_inplace_arguments(self, args):
+        if not all(os.path.isfile(path) for path in args):
+            raise DataError('Given input is not a file.')
+
+    def _validate_default_mode_arguments(self, args):
+        if len(args) not in (1, 2):
             raise DataError('Default mode requires 1 or 2 arguments.')
+        if not os.path.isfile(args[0]):
+            raise DataError('Given input is not a file.')

     def _validate_format(self, args, format, inplace, recursive, **others):
         if not format:
=======================================
--- /utest/tidy/test_argument_validation.py     Tue Dec 18 05:38:43 2012
+++ /utest/tidy/test_argument_validation.py     Wed Jan 16 03:54:26 2013
@@ -12,8 +12,8 @@
         self._validate(format='invalid', error="Invalid format 'INVALID'.")

     def test_invalid_implicit_format(self):
- self._validate(args=['x.txt', 'y.inv'], error="Invalid format 'INV'.")
-        self._validate(args=['x.txt', 'inv'], error="Invalid format ''.")
+ self._validate(args=[__file__, 'y.inv'], error="Invalid format 'INV'.")
+        self._validate(args=[__file__, 'inv'], error="Invalid format ''.")

     def test_invalid_space_count(self):
         error = '--spacecount must be an integer greater than 1.'
@@ -33,9 +33,9 @@
                                    Stubbed().execute_cli, args)

     def test_default_mode_accepts_one_or_two_arguments(self):
-        self._validate(args=['1'])
-        self._validate(args=['1', '2.txt'])
-        self._validate(args=['1', '2', '3'],
+        self._validate(args=[__file__])
+        self._validate(args=[__file__, '2.txt'])
+        self._validate(args=[__file__, '2', '3'],
                        error='Default mode requires 1 or 2 arguments.')

     def test_recursive_accepts_only_one_argument(self):
@@ -44,7 +44,14 @@

     def test_inplace_accepts_one_or_more_arguments(self):
         for count in range(1, 10):
-            self._validate(inplace=True, args=['a']*count)
+            self._validate(inplace=True, args=[__file__]*count)
+
+    def test_default_and_inplace_modes_requires_inputs_to_be_files(self):
+        error = 'Given input is not a file.'
+        self._validate(args=['.'], error=error)
+        self._validate(args=['non_existing.txt'], error=error)
+        self._validate(inplace=True, args=[__file__, '.'], error=error)
+ self._validate(inplace=True, args=[__file__, 'nonex.txt'], error=error)

     def test_recursive_requires_input_to_be_directory(self):
         self._validate(recursive=True,
@@ -61,7 +68,7 @@
                        error="Invalid line separator 'invalid'.")

     def _validate(self, inplace=False, recursive=False, format=None,
-                  spacecount=None, lineseparator=None, args=['a_file.txt'],
+                  spacecount=None, lineseparator=None, args=[__file__],
                   error=None):
opts = {'inplace': inplace, 'recursive': recursive, 'format': format,
                 'spacecount': spacecount, 'lineseparator': lineseparator}

==============================================================================
Revision: 7b317123001d
Branch:   default
Author:   Pekka Klärck
Date:     Wed Jan 16 05:35:36 2013
Log: Tidy: Moved argument validation into a separate class and slightly enhanced it.

Update issue 1333
Status: Done
Refactored argument validation in general. This issue is done.
http://code.google.com/p/robotframework/source/detail?r=7b317123001d

Modified:
 /src/robot/tidy.py
 /utest/tidy/test_argument_validation.py

=======================================
--- /src/robot/tidy.py  Wed Jan 16 03:54:26 2013
+++ /src/robot/tidy.py  Wed Jan 16 05:35:36 2013
@@ -187,40 +187,46 @@
             self.console(output)

     def validate(self, opts, args):
-        self._validate_mode_and_arguments(args, **opts)
-        opts['format'] = self._validate_format(args, **opts)
-        opts['lineseparator'] = self._validate_line_sep(**opts)
+        validator = ArgumentValidator()
+        validator.mode_and_arguments(args, **opts)
+        opts['format'] = validator.format(args, **opts)
+        opts['lineseparator'] = validator.line_sep(**opts)
         if not opts['spacecount']:
             opts.pop('spacecount')
         else:
- opts['spacecount'] = self._validate_spacecount(opts['spacecount'])
+            opts['spacecount'] = validator.spacecount(opts['spacecount'])
         return opts, args

- def _validate_mode_and_arguments(self, args, inplace, recursive, **others):
-        if inplace and recursive:
- raise DataError('--recursive and --inplace can not be used together.')
-        elif recursive:
-            self._validate_recursive_arguments(args)
-        elif inplace:
-            self._validate_inplace_arguments(args)
-        else:
-            self._validate_default_mode_arguments(args)

-    def _validate_recursive_arguments(self, args):
-        if len(args) != 1 or not os.path.isdir(args[0]):
- raise DataError('--recursive requires exactly one directory as argument.')
+class ArgumentValidator(object):

-    def _validate_inplace_arguments(self, args):
+    def mode_and_arguments(self, args, recursive, inplace, **others):
+        validators = {(True, True): self._recursive_and_inplace_together,
+                      (True, False): self._recursive_mode_arguments,
+                      (False, True): self._inplace_mode_arguments,
+                      (False, False): self._default_mode_arguments}
+        validators[(recursive, inplace)](args)
+
+    def _recursive_and_inplace_together(self, args):
+ raise DataError('--recursive and --inplace can not be used together.')
+
+    def _recursive_mode_arguments(self, args):
+        if len(args) != 1:
+            raise DataError('--recursive requires exactly one argument.')
+        if not os.path.isdir(args[0]):
+ raise DataError('--recursive requires input to be a directory.')
+
+    def _inplace_mode_arguments(self, args):
         if not all(os.path.isfile(path) for path in args):
-            raise DataError('Given input is not a file.')
+            raise DataError('--inplace requires inputs to be files.')

-    def _validate_default_mode_arguments(self, args):
+    def _default_mode_arguments(self, args):
         if len(args) not in (1, 2):
             raise DataError('Default mode requires 1 or 2 arguments.')
         if not os.path.isfile(args[0]):
-            raise DataError('Given input is not a file.')
+            raise DataError('Default mode requires input to be a file.')

-    def _validate_format(self, args, format, inplace, recursive, **others):
+    def format(self, args, format, inplace, recursive, **others):
         if not format:
             if inplace or recursive or len(args) < 2:
                 return None
@@ -230,16 +236,14 @@
             raise DataError("Invalid format '%s'." % format)
         return format

-    def _validate_line_sep(self, lineseparator, **others):
-        if not lineseparator:
-            return os.linesep
+    def line_sep(self, lineseparator, **others):
         values = {'native': os.linesep, 'windows': '\r\n', 'unix': '\n'}
         try:
-            return values[lineseparator.lower()]
+            return values[(lineseparator or 'native').lower()]
         except KeyError:
             raise DataError("Invalid line separator '%s'." % lineseparator)

-    def _validate_spacecount(self, spacecount):
+    def spacecount(self, spacecount):
         try:
             spacecount = int(spacecount)
             if spacecount < 2:
=======================================
--- /utest/tidy/test_argument_validation.py     Wed Jan 16 03:54:26 2013
+++ /utest/tidy/test_argument_validation.py     Wed Jan 16 05:35:36 2013
@@ -3,11 +3,23 @@

 from robot.errors import DataError
 from robot.tidy import TidyCommandLine
-from robot.utils.asserts import assert_raises_with_msg, assert_equals
+from robot.utils.asserts import assert_raises_with_msg, assert_equals, assert_true


 class TestArgumentValidation(unittest.TestCase):

+    def test_valid_explicit_format(self):
+        opts, _ = self._validate(format='txt')
+        assert_equals(opts['format'], 'TXT')
+
+    def test_valid_implicit_format(self):
+        opts, _ = self._validate(args=[__file__, 'out.robot'])
+        assert_equals(opts['format'], 'ROBOT')
+
+    def test_no_format(self):
+        opts, _ = self._validate()
+        assert_equals(opts['format'], None)
+
     def test_invalid_explicit_format(self):
         self._validate(format='invalid', error="Invalid format 'INVALID'.")

@@ -15,6 +27,14 @@
self._validate(args=[__file__, 'y.inv'], error="Invalid format 'INV'.")
         self._validate(args=[__file__, 'inv'], error="Invalid format ''.")

+    def test_no_space_count(self):
+        opts, _ = self._validate()
+        assert_true('spacecount' not in opts)
+
+    def test_valid_space_count(self):
+        opts, _ = self._validate(spacecount='42')
+        assert_equals(opts['spacecount'], 42)
+
     def test_invalid_space_count(self):
         error = '--spacecount must be an integer greater than 1.'
         self._validate(spacecount='not a number', error=error)
@@ -39,23 +59,26 @@
                        error='Default mode requires 1 or 2 arguments.')

     def test_recursive_accepts_only_one_argument(self):
-        self._validate(recursive=True, args=['a', 'b'],
- error='--recursive requires exactly one directory as argument.')
+        self._validate(recursive=True, args=['.', '..'],
+                       error='--recursive requires exactly one argument.')

     def test_inplace_accepts_one_or_more_arguments(self):
         for count in range(1, 10):
             self._validate(inplace=True, args=[__file__]*count)

-    def test_default_and_inplace_modes_requires_inputs_to_be_files(self):
-        error = 'Given input is not a file.'
+    def test_default_mode_requires_input_to_be_file(self):
+        error = 'Default mode requires input to be a file.'
         self._validate(args=['.'], error=error)
         self._validate(args=['non_existing.txt'], error=error)
+
+    def test_inplace_requires_inputs_to_be_files(self):
+        error = '--inplace requires inputs to be files.'
         self._validate(inplace=True, args=[__file__, '.'], error=error)
self._validate(inplace=True, args=[__file__, 'nonex.txt'], error=error)

     def test_recursive_requires_input_to_be_directory(self):
         self._validate(recursive=True,
- error='--recursive requires exactly one directory as argument.') + error='--recursive requires input to be a directory.')

     def test_line_separator(self):
         for input, expected in [(None, os.linesep), ('Native', os.linesep),

Reply via email to