Revision: 0f8820fdfb9a
Author:   Robot Framework Developers <[email protected]>
Date:     Mon Jan 16 04:01:53 2012
Log:      tidy: implemented --inplace option
http://code.google.com/p/robotframework/source/detail?r=0f8820fdfb9a

Modified:
 /atest/robot/tidy/tidy.txt
 /atest/testdata/tidy/golden_pipes.txt
 /src/robot/tidy.py

=======================================
--- /atest/robot/tidy/tidy.txt  Sun Jan 15 23:03:29 2012
+++ /atest/robot/tidy/tidy.txt  Mon Jan 16 04:01:53 2012
@@ -3,9 +3,12 @@
 Library           TidyLib.py    ${INTERPRETER}
 Library           OperatingSystem
 Resource          atest_resource.txt
+Suite Setup       Create Directory     ${TIDYDIR}
+Suite Teardown    Remove Directory     ${TIDYDIR}    recursive=True

 *** Variables ***
 ${DATADIR}        ${CURDIR}/../../testdata/tidy
+${TIDYDIR}        ${TEMPDIR}/tidy-test

 *** Test cases ***
 Tidying single test case file
@@ -24,6 +27,29 @@

 Tidying single init file
Run tidy and check result ${DATADIR}/__init__.txt ${EMPTY} ${DATADIR}/__init__.txt
+
+Tidying single file in place
+    [Setup]    Copy File    ${DATADIR}/golden.txt    ${TIDYDIR}/golden.txt
+ Run tidy and check result ${TIDYDIR}/golden.txt --inplace --use-pipes ${DATADIR}/golden_pipes.txt
+    Check file count    ${TIDYDIR}    *.txt    1
+    [Teardown]    Empty Directory     ${TIDYDIR}
+
+Tidying single file in place and change format
+    [Setup]    Copy File    ${DATADIR}/golden.txt    ${TIDYDIR}/golden.txt
+ Run tidy and check result ${TIDYDIR}/golden.txt --inplace --format html ${DATADIR}/golden.html
+    Check file count    ${TIDYDIR}    *.html    1
+    Check file count    ${TIDYDIR}    *.txt    0
+    [Teardown]    Empty Directory     ${TIDYDIR}
+
+Tidying many files in place
+    [Setup]    Copy Golden Files
+    List Directory      ${TIDYDIR}
+    Run tidy    ${TIDYDIR}/golden*    --inplace --format html
+    List Directory      ${TIDYDIR}
+    Check file count    ${TIDYDIR}    *.html    2
+    Check file count    ${TIDYDIR}    *.txt    0
+    Check file count    ${TIDYDIR}    *.tsv    0
+    [Teardown]    Empty Directory     ${TIDYDIR}

 Tidying directory
     [Setup]    Copy Directory    ${DATADIR}/tests    ${TEMPDIR}/tests
@@ -49,3 +75,6 @@
     ${files}=    Count Files In Directory    ${directory}    ${pattern}
     Should Be Equal As Numbers    ${files}    ${expected count}

+Copy Golden Files
+     Copy File    ${DATADIR}/golden_pipes.txt    ${TIDYDIR}/
+     Copy File    ${DATADIR}/golden.tsv    ${TIDYDIR}/
=======================================
--- /atest/testdata/tidy/golden_pipes.txt       Fri Jan 13 04:28:56 2012
+++ /atest/testdata/tidy/golden_pipes.txt       Mon Jan 16 04:01:53 2012
@@ -15,8 +15,8 @@
 |    | My TC Step 2 | my step 2 arg | second \ arg | # step 2 comment |
 |    | [Teardown] | 1 minute | args |

- | Another Test |
- |    | No Operation |
+| Another Test |
+|    | No Operation |

 | *** Keywords *** |
 | My Keyword |
=======================================
--- /src/robot/tidy.py  Sun Jan 15 23:02:28 2012
+++ /src/robot/tidy.py  Mon Jan 16 04:01:53 2012
@@ -27,9 +27,9 @@
 import sys
 from StringIO import StringIO

-from robot import utils
+from robot.utils import ArgumentParser
 from robot.errors import DataError
-from robot.parsing import TestData, ResourceFile, TestDataDirectory
+from robot.parsing import ResourceFile, TestDataDirectory, TestCaseFile
 from robot.parsing.populators import FromFilePopulator


@@ -38,18 +38,22 @@
     def __init__(self, **options):
         self._options = options

-    def files(self, files):
-        for f in files:
-            yield self._save_file(f)
-
-    def _save_file(self, path):
+    def file(self, path):
         data = self._create_datafile(path)
         output = StringIO()
         data.save(output=output, **self._options)
         return output.getvalue()

     def directory(self, path):
-        self._save_recursively(self._create_datafile(path))
+        self._save_recursively(self._create_data_directory(path))
+
+    def inplace(self, path):
+        data = self._create_datafile(path)
+        os.remove(data.source)
+        data.save(**self._options)
+
+    def _create_data_directory(self, path):
+        return TestDataDirectory(source=path).populate()

     def _save_recursively(self, data):
         init_file = getattr(data, 'initfile', None)
@@ -69,7 +73,7 @@
             FromFilePopulator(data).populate(source)
             return data
         try:
-            return TestData(source=source)
+            return TestCaseFile(source=source).populate()
         except DataError:
             try:
                 return ResourceFile(source=source).populate()
@@ -77,24 +81,36 @@
                 raise DataError("Invalid data source '%s'" % source)


-def _parse_args():
-    parser = utils.ArgumentParser(__doc__)
-    return parser.parse_args(sys.argv[1:], help='help', check_args=True)
-
-def main():
-    opts, args = _parse_args()
-    tidy = Tidy(format=opts['format'], pipe_separated=opts['use-pipes'])
-    if not opts['inplace'] or not opts['recursive']:
-        for output in tidy.files(args):
-            print output
-    if opts['recursive']:
-        tidy.directory(args[0])
+class TidyCommandLine(object):
+
+    def __init__(self, usage):
+        self._parser = ArgumentParser(usage)
+
+    def run(self, args):
+        options, inputs = self._parse_args(args)
+        tidy = Tidy(format=options['format'],
+                    pipe_separated=options['use-pipes'])
+        if options['recursive']:
+            tidy.directory(inputs[0])
+        elif options['inplace']:
+            for source in inputs:
+                tidy.inplace(source)
+        else:
+            return tidy.file(inputs[0])
+
+    def _parse_args(self, args):
+        options, sources = self._parser.parse_args(args, help='help')
+        if not options['inplace'] and len(sources) > 1:
+            raise DataError('Wrong number of input files')
+        return options, sources


 if __name__ == '__main__':
     try:
-        main()
+        output = TidyCommandLine(__doc__).run(sys.argv[1:])
+        if output:
+            print output
     except DataError, err:
         print __doc__
-        sys.exit(unicode(err), 1)
+        sys.exit(unicode(err))
     sys.exit(0)

Reply via email to