-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methot to replace Python format for PY2 and PY3 (#1468)
/path/{to}/file-{index}.log types of paths causes problems for Python format method. This is now fixed. Fixes #1452
- Loading branch information
Showing
6 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2008-2011 Nokia Networks | ||
# Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors | ||
# Copyright 2016- Robot Framework Foundation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from SeleniumLibrary.base.robotlibcore import PY2 | ||
|
||
|
||
# Cab be deleted when Python 2 is not anymore supported. | ||
def _format_path(file_path, index): | ||
if PY2: | ||
import string | ||
return string.Formatter().vformat(file_path, (), _SafeFormatter(index=index)) | ||
return file_path.format_map(_SafeFormatter(index=index)) | ||
|
||
|
||
class _SafeFormatter(dict): | ||
|
||
def __missing__(self, key): | ||
return '{%s}' % key |
11 changes: 11 additions & 0 deletions
11
...st/api/approved_files/test_filepath_unusual_characters.test_normal_file_path.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Different file paths. | ||
|
||
0) /foo/file.log | ||
1) /foo/file-1.log | ||
2) /foo/file-0001.log | ||
3) /foo/file-{foo}.log | ||
4) /{foo}/file-1.log | ||
5) /foo/file-001.log | ||
6) /foo/1234-file-1234.log | ||
7) /foo/file-{in dex}.log | ||
8) /foo/file-{in@dex}.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import os | ||
|
||
import pytest | ||
from robot.utils import JYTHON | ||
|
||
try: | ||
from approvaltests.approvals import verify_all | ||
from approvaltests.reporters.generic_diff_reporter_factory import GenericDiffReporterFactory | ||
except ImportError: | ||
if JYTHON: | ||
verify = None | ||
GenericDiffReporterFactory = None | ||
else: | ||
raise | ||
|
||
from SeleniumLibrary.utils.path_formatter import _format_path | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def reporter(): | ||
if JYTHON: | ||
return None | ||
else: | ||
path = os.path.dirname(__file__) | ||
reporter_json = os.path.abspath(os.path.join(path, '..', 'approvals_reporters.json')) | ||
factory = GenericDiffReporterFactory() | ||
factory.load(reporter_json) | ||
return factory.get_first_working() | ||
|
||
|
||
@pytest.mark.skipif(JYTHON, reason='ApprovalTest does not work with Jython') | ||
def test_normal_file_path(reporter): | ||
results = [] | ||
results.append(_format_path('/foo/file.log', 1)) | ||
results.append(_format_path('/foo/file-{index}.log', 1)) | ||
results.append(_format_path('/foo/file-{index}.log', '0001')) | ||
results.append(_format_path('/foo/file-{foo}.log', 1)) | ||
results.append(_format_path('/{foo}/file-{index}.log', 1)) | ||
results.append(_format_path('/foo/file-{index:03}.log', 1)) | ||
results.append(_format_path('/foo/{index}-file-{index}.log', '1234')) | ||
results.append(_format_path('/foo/file-{in dex}.log', '1234')) | ||
results.append(_format_path('/foo/file-{in@dex}.log', '1234')) | ||
verify_all('Different file paths.', results, reporter=reporter) |