3 new revisions:

Revision: 2e1283d5cbab
Branch:   default
Author:   Pekka Klärck
Date:     Thu Apr 17 18:20:09 2014 UTC
Log:      Clean up OS.Create File tests and code
http://code.google.com/p/robotframework/source/detail?r=2e1283d5cbab

Revision: 908f0c51ae6d
Branch:   default
Author:   Pekka Klärck
Date:     Tue Apr 22 08:16:45 2014 UTC
Log:      OSLib: Fixed reference to Create File in Append To File docs.
http://code.google.com/p/robotframework/source/detail?r=908f0c51ae6d

Revision: 64d3fc42e588
Branch:   default
Author:   Pekka Klärck
Date:     Tue Apr 22 08:18:41 2014 UTC
Log:      OSLib: New Create Binary File keyword...
http://code.google.com/p/robotframework/source/detail?r=64d3fc42e588

==============================================================================
Revision: 2e1283d5cbab
Branch:   default
Author:   Pekka Klärck
Date:     Thu Apr 17 18:20:09 2014 UTC
Log:      Clean up OS.Create File tests and code
http://code.google.com/p/robotframework/source/detail?r=2e1283d5cbab

Modified:
 /atest/robot/standard_libraries/operating_system/create_file.txt
 /atest/testdata/standard_libraries/operating_system/create_file.txt
 /atest/testdata/standard_libraries/operating_system/os_resource.txt
 /src/robot/libraries/OperatingSystem.py

=======================================
--- /atest/robot/standard_libraries/operating_system/create_file.txt Wed May 4 11:17:01 2011 UTC +++ /atest/robot/standard_libraries/operating_system/create_file.txt Thu Apr 17 18:20:09 2014 UTC
@@ -1,28 +1,32 @@
 *** Settings ***
-Suite Setup Run Tests ${EMPTY} standard_libraries/operating_system/create_file.txt
-Force Tags      regression  pybot  jybot
-Resource        atest_resource.txt
+Suite Setup Run Tests ${EMPTY} standard_libraries/operating_system/create_file.txt
+Force Tags       regression    pybot    jybot
+Resource         atest_resource.txt

 *** Test Cases ***
+Create File With Default Content
+    Check Test Case    ${TESTNAME}

-Create File
-    Check Test Case  ${TESTNAME}
+Create File With Content
+    Check Test Case    ${TESTNAME}
+
+Create Non-ASCII File With Default Encoding
+    Check Test Case    ${TESTNAME}

 Create File With Encoding
-    Check Test Case  ${TESTNAME}
+    Check Test Case    ${TESTNAME}

-Create Non-Ascii File
-    Check Test Case  ${TESTNAME}
+Create File With Non-ASCII Name
+    Check Test Case    ${TESTNAME}

 Create File With Space In Name
-    Check Test Case  ${TESTNAME}
+    Check Test Case    ${TESTNAME}
+
+Create File To Non-Existing Directory
+    Check Test Case    ${TESTNAME}

-Create File Fails If Encoding Is Incorrect
-    Check Test Case  ${TESTNAME}
+Creating File Fails If Encoding Is Incorrect
+    Check Test Case    ${TESTNAME}

 Append To File
-    Check Test Case  ${TESTNAME}
-
-Create File To Non-Existing Dir
-    Check Test Case  ${TESTNAME}
-
+    Check Test Case    ${TESTNAME}
=======================================
--- /atest/testdata/standard_libraries/operating_system/create_file.txt Fri Oct 12 06:37:15 2012 UTC +++ /atest/testdata/standard_libraries/operating_system/create_file.txt Thu Apr 17 18:20:09 2014 UTC
@@ -1,54 +1,56 @@
 *** Settings ***
-Suite Setup     Create Non-ASCII Input
 Test Setup      Create Base Test Directory
 Suite Teardown  Remove Base Test Directory
 Resource        os_resource.txt

 *** Test Cases ***
-Create File
+Create File With Default Content
     Create File    ${TESTFILE}
     Verify File    ${TESTFILE}    ${EMPTY}
-    Create File    ${TESTFILE}    This is content in\n3\nlines
-    Verify File    ${TESTFILE}    This is content in\n3\nlines
+
+Create File With Content
+    [Template]    Create and Verify File
+    ${EMPTY}
+    Content in one line
+    This is content in\n3\nlines
+    Nön-ÄSCÏÏ Cöntënt
+
+Create Non-ASCII File With Default Encoding
+    Create File    ${TESTFILE}    Nön-ÄSCÏÏ Cöntënt
+    Verify File    ${TESTFILE}    Nön-ÄSCÏÏ Cöntënt

 Create File With Encoding
     [Template]    Create and Verify File
     Just ASCII     ASCII
     Hyvää yötä!    UTF-8
     Hyvää yötä!    ISO-8859-1
-    ${UNICODE}     UTF-8
-    ${UNICODE}     ISO-8859-5
+    Спасибо        UTF-8
+    Спасибо        ISO-8859-5

-Create Non-Ascii File
-    Create And Verify File    ASCII content    file=${NON ASCII}
-    Create And Verify File    ${UNICODE}    file=${NON ASCII}
+Create File With Non-ASCII Name
+    [Template]    Create and Verify File
+    ASCII content    file=${NON ASCII}
+    Спасибо          file=${NON ASCII}

 Create File With Space In Name
     Create And Verify File    file=${WITH SPACE}

-Create File Fails If Encoding Is Incorrect
+Create File To Non-Existing Directory
+    Create And Verify File    file=${TESTDIR}${/}file.txt
+
+Creating File Fails If Encoding Is Incorrect
     [Documentation]    FAIL REGEXP: Unicode(Encode|)Error: .*
     Create File    ${TESTFILE}    Hyvää yötä!    ASCII

 Append To File
     Append To File    ${TESTFILE}    First line\n
     Append To File    ${TESTFILE}    Second line\n    ASCII
-    Append To File    ${TESTFILE}    ${UNICODE}
-    Verify File    ${TESTFILE}    First line\nSecond line\n${UNICODE}
-
-Create File To Non-Existing Dir
-    Create File    ${TESTDIR}${/}file.txt    my content
-    Verify File    ${TESTDIR}${/}file.txt    my content
+    Append To File    ${TESTFILE}    ${EMPTY}
+    Append To File    ${TESTFILE}    Lääst läin
+    Verify File    ${TESTFILE}    First line\nSecond line\nLääst läin

 *** Keywords ***
-Create Non-ASCII Input
-    ${BYTES} =    Evaluate    '\xc1\xdf\xd0\xe1\xd8\xd1\xde'
-    ${UNICODE} =    Evaluate    '${BYTES}'.decode('ISO-8859-5')
-    Set Suite Variable    ${BYTES}
-    Set Suite Variable    ${UNICODE}
-
-Create And Verify Binary File
-    [Arguments]   ${content}
-    Create Binary File    ${TESTFILE}    ${content}
-    ${result} =    Get Binary File    ${TESTFILE}
-    Should Be Equal    ${result}    ${content}
+Create And Verify File
+ [Arguments] ${content}=content ${encoding}=UTF-8 ${file}=${TESTFILE}
+    Create File    ${file}    ${content}    ${encoding}
+    Verify File    ${file}    ${content}    ${encoding}
=======================================
--- /atest/testdata/standard_libraries/operating_system/os_resource.txt Tue Nov 5 08:46:37 2013 UTC +++ /atest/testdata/standard_libraries/operating_system/os_resource.txt Thu Apr 17 18:20:09 2014 UTC
@@ -14,11 +14,6 @@
 ${ANYDIR}        .*[/\\\\]

 *** Keywords ***
-Create And Verify File
-    [Arguments]  ${content}=content  ${encoding}=UTF-8  ${file}=${TESTFILE}
-    Create File  ${file}  ${content}  ${encoding}
-    Verify File  ${file}  ${content}  ${encoding}
-
 Verify File
     [Arguments]  ${path}  ${expected}  ${encoding}=UTF-8
     ${content} =  Log File  ${path}  ${encoding}
=======================================
--- /src/robot/libraries/OperatingSystem.py     Thu Jan 30 14:54:15 2014 UTC
+++ /src/robot/libraries/OperatingSystem.py     Thu Apr 17 18:20:09 2014 UTC
@@ -608,7 +608,7 @@
         and use `File Should Not Exist` if you want to avoid overwriting
         existing files.
         """
-        path = self._write_to_file(path, content, encoding, 'w')
+        path = self._write_to_file(path, content, encoding)
         self._link("Created file '%s'", path)

     def append_to_file(self, path, content, encoding='UTF-8'):
@@ -617,19 +617,17 @@
         If the file does not exists, this keyword works exactly the same
         way as `Create File With Encoding`.
         """
-        path = self._write_to_file(path, content, encoding, 'a')
+        path = self._write_to_file(path, content, encoding, mode='a')
         self._link("Appended to file '%s'", path)

-    def _write_to_file(self, path, content, encoding, mode):
+    def _write_to_file(self, path, content, encoding, mode='w'):
         path = self._absnorm(path)
         parent = os.path.dirname(path)
         if not os.path.exists(parent):
             os.makedirs(parent)
-        f = open(path, mode+'b')
-        try:
-            f.write(content.encode(encoding))
-        finally:
-            f.close()
+        content = content.encode(encoding)
+        with open(path, mode+'b') as f:
+            f.write(content)
         return path

     def remove_file(self, path):

==============================================================================
Revision: 908f0c51ae6d
Branch:   default
Author:   Pekka Klärck
Date:     Tue Apr 22 08:16:45 2014 UTC
Log:      OSLib: Fixed reference to Create File in Append To File docs.
http://code.google.com/p/robotframework/source/detail?r=908f0c51ae6d

Modified:
 /src/robot/libraries/OperatingSystem.py

=======================================
--- /src/robot/libraries/OperatingSystem.py     Thu Apr 17 18:20:09 2014 UTC
+++ /src/robot/libraries/OperatingSystem.py     Tue Apr 22 08:16:45 2014 UTC
@@ -615,7 +615,7 @@
         """Appends the given contend to the specified file.

         If the file does not exists, this keyword works exactly the same
-        way as `Create File With Encoding`.
+        way as `Create File`.
         """
         path = self._write_to_file(path, content, encoding, mode='a')
         self._link("Appended to file '%s'", path)

==============================================================================
Revision: 64d3fc42e588
Branch:   default
Author:   Pekka Klärck
Date:     Tue Apr 22 08:18:41 2014 UTC
Log:      OSLib: New Create Binary File keyword

Also some enhancements to Create File docs.

Update issue 1694
Status: Done
Owner: pekka.klarck
Implemented, tested, and documented new `Create Binary File` keyword.
http://code.google.com/p/robotframework/source/detail?r=64d3fc42e588

Modified:
 /atest/robot/standard_libraries/operating_system/create_file.txt
 /atest/testdata/standard_libraries/operating_system/create_file.txt
 /src/robot/libraries/OperatingSystem.py

=======================================
--- /atest/robot/standard_libraries/operating_system/create_file.txt Thu Apr 17 18:20:09 2014 UTC +++ /atest/robot/standard_libraries/operating_system/create_file.txt Tue Apr 22 08:18:41 2014 UTC
@@ -27,6 +27,15 @@

 Creating File Fails If Encoding Is Incorrect
     Check Test Case    ${TESTNAME}
+
+Create Binary File Using Bytes
+    Check Test Case    ${TESTNAME}
+
+Create Binary File Using Unicode
+    Check Test Case    ${TESTNAME}
+
+Creating Binary File Using Unicode With Ordinal > 255 Fails
+    Check Test Case    ${TESTNAME}

 Append To File
     Check Test Case    ${TESTNAME}
=======================================
--- /atest/testdata/standard_libraries/operating_system/create_file.txt Thu Apr 17 18:20:09 2014 UTC +++ /atest/testdata/standard_libraries/operating_system/create_file.txt Tue Apr 22 08:18:41 2014 UTC
@@ -42,6 +42,24 @@
     [Documentation]    FAIL REGEXP: Unicode(Encode|)Error: .*
     Create File    ${TESTFILE}    Hyvää yötä!    ASCII

+Create Binary File Using Bytes
+    [Template]    Create And Verify Binary File Using Bytes
+    ${EMPTY}
+    Hello, world!
+    Hyvää yötä!
+    \x00\x01\xe4\xff
+
+Create Binary File Using Unicode
+    [Template]    Create And Verify Binary File Using Unicode
+    ${EMPTY}
+    Hello, world!
+    Hyvää yötä!
+    \x00\x01\xe4\xff
+
+Creating Binary File Using Unicode With Ordinal > 255 Fails
+    [Documentation]    FAIL STARTS: ValueError:
+    Create Binary File    ${TESTFILE}    \u0100
+
 Append To File
     Append To File    ${TESTFILE}    First line\n
     Append To File    ${TESTFILE}    Second line\n    ASCII
@@ -54,3 +72,20 @@
[Arguments] ${content}=content ${encoding}=UTF-8 ${file}=${TESTFILE}
     Create File    ${file}    ${content}    ${encoding}
     Verify File    ${file}    ${content}    ${encoding}
+
+Create And Verify Binary File Using Bytes
+    [Arguments]    ${content}    ${file}=${TESTFILE}
+    ${content} =    Convert To Bytes    ${content}
+    Create Binary File    ${file}    ${content}
+    Verify Binary File    ${file}    ${content}
+
+Create And Verify Binary File Using Unicode
+    [Arguments]    ${content}    ${file}=${TESTFILE}
+    Create Binary File    ${file}    ${content}
+    ${expected} =    Convert To Bytes    ${content}
+    Verify Binary File    ${file}    ${expected}
+
+Verify Binary File
+    [Arguments]    ${path}    ${expected}
+    ${content} =    Get Binary File    ${file}
+    Should Be Equal    ${content}    ${expected}
=======================================
--- /src/robot/libraries/OperatingSystem.py     Tue Apr 22 08:16:45 2014 UTC
+++ /src/robot/libraries/OperatingSystem.py     Tue Apr 22 08:18:41 2014 UTC
@@ -604,13 +604,45 @@
If the directory where to create file does not exist it, and possible
         intermediate missing directories, are created.

-        Use `Append To File` if you want to append to an existing file,
-        and use `File Should Not Exist` if you want to avoid overwriting
-        existing files.
+        Examples:
+        | Create File | ${dir}/example.txt | Hello, world!      |         |
+        | Create File | ${path}            | Hyv\\xe4 esimerkki | latin-1 |
+
+        Use `Append To File` if you want to append to an existing file
+ and `Create Binary File` if you need to write bytes without encoding.
+        `File Should Not Exist` can be used to avoid overwriting existing
+        files.
         """
         path = self._write_to_file(path, content, encoding)
         self._link("Created file '%s'", path)

+    def create_binary_file(self, path, content):
+        """Creates a binary file with the given content.
+
+ If content is given as a Unicode string, it is first converted to bytes + character by character. All characters with ordinal below 256 can be
+        used and are converted to bytes with same values.
+
+ Byte strings, and possible other types, are written to the file as is.
+
+ If the directory where to create file does not exist it, and possible
+        intermediate missing directories, are created.
+
+        Examples:
+        | Create Binary File | ${dir}/example.png | ${image content}     |
+        | Create Binary File | ${path}            | \\x01\\x00\\xe4\\x00 |
+
+        Use `Create File` if you want to create a text file using a certain
+        encoding. `File Should Not Exist` can be used to avoid overwriting
+        existing files.
+
+        New in Robot Framework 2.8.5.
+        """
+        if isinstance(content, unicode):
+            content = ''.join(chr(ord(c)) for c in content)
+        path = self._write_to_file(path, content)
+        self._link("Created binary file '%s'", path)
+
     def append_to_file(self, path, content, encoding='UTF-8'):
         """Appends the given contend to the specified file.

@@ -620,12 +652,13 @@
         path = self._write_to_file(path, content, encoding, mode='a')
         self._link("Appended to file '%s'", path)

-    def _write_to_file(self, path, content, encoding, mode='w'):
+    def _write_to_file(self, path, content, encoding=None, mode='w'):
         path = self._absnorm(path)
         parent = os.path.dirname(path)
         if not os.path.exists(parent):
             os.makedirs(parent)
-        content = content.encode(encoding)
+        if encoding:
+            content = content.encode(encoding)
         with open(path, mode+'b') as f:
             f.write(content)
         return path

--

--- You received this message because you are subscribed to the Google Groups "robotframework-commit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to robotframework-commit+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to