Ejemplo n.º 1
0
    def test_inference_git_submodule(self):
        with tempfile.TemporaryDirectory() as tempdirname:
            temppath = pathlib.PurePath(tempdirname)

            self._run_command(
                ['git', 'init',
                 str(temppath.joinpath("submod"))])
            self._run_command(
                ['git', 'commit', '--allow-empty', '--message', 'test commit'],
                cwd=str(temppath.joinpath("submod")))

            self._run_command(
                ['git', 'init',
                 str(temppath.joinpath("gitrepo"))])
            self._run_command([
                'git', 'submodule', 'add',
                str(temppath.joinpath("submod")), 'submod'
            ],
                cwd=str(temppath.joinpath("gitrepo")))

            base = str(temppath.joinpath("gitrepo"))
            relpath = os.path.join('submod', 'foo', 'bar', 'baz')
            abspath = os.path.join(base, 'submod', 'foo', 'bar', 'baz')

            n = FilePathNormalizer()
            self.assertEqual(relpath, n.relativize(abspath))
Ejemplo n.º 2
0
    def test_base_path(self):
        base = os.path.abspath('base')
        relpath = os.path.join('foo', 'bar', 'baz')
        abspath = os.path.join(base, 'foo', 'bar', 'baz')

        n = FilePathNormalizer(base_path=base)
        self.assertEqual(relpath, n.relativize(relpath))
        self.assertEqual(relpath, n.relativize(abspath))
Ejemplo n.º 3
0
    def test_inference_git(self):
        with tempfile.TemporaryDirectory() as tempdirname:
            temppath = pathlib.PurePath(tempdirname)
            base = str(temppath.joinpath("gitrepo"))
            relpath = os.path.join('foo', 'bar', 'baz')
            abspath = os.path.join(base, 'foo', 'bar', 'baz')

            self._run_command(['git', 'init', base])

            n = FilePathNormalizer()
            self.assertEqual(relpath, n.relativize(abspath))
Ejemplo n.º 4
0
    def test_normalize_path(self):
        relpath = os.path.join('foo', 'bar', 'baz')
        non_normalized = os.path.join('foo', 'bar', 'omit', '..', 'baz')

        n = FilePathNormalizer()
        self.assertEqual(relpath, n.relativize(non_normalized))
Ejemplo n.º 5
0
 def test_relative_path(self):
     n = FilePathNormalizer()
     relpath = os.path.join('foo', 'bar', 'baz')
     self.assertEqual(relpath, n.relativize(relpath))
Ejemplo n.º 6
0
 def __init__(self, client):
     self.client = client
     self.file_path_normalizer = FilePathNormalizer(
         base_path=client.base_path,
         no_base_path_inference=client.no_base_path_inference)
Ejemplo n.º 7
0
class JSONReportParser:
    """
        client: launchable.RecordTests
    """
    def __init__(self, client):
        self.client = client
        self.file_path_normalizer = FilePathNormalizer(
            base_path=client.base_path,
            no_base_path_inference=client.no_base_path_inference)

    def parse_func(self,
                   report_file: str) -> Generator[CaseEventType, None, None]:
        """
        example of JSON format report
        [
        {
            "uri": "features/foo/is_it_friday_yet.feature",
            "id": "is-it-friday-yet?",
            "keyword": "Feature",
            "name": "Is it Friday yet?",
            "description": "  Everybody wants to know when it's Friday",
            "line": 1,
            "elements": [
            {
                "id": "is-it-friday-yet?;today-is-or-is-not-friday;;2",
                "keyword": "Scenario Outline",
                "name": "Today is or is not Friday",
                "description": "",
                "line": 11,
                "type": "scenario",
                "steps": [
                {
                    "keyword": "Given ",
                    "name": "today is \"Friday\"",
                    "line": 5,
                    "match": {
                    "location": "features/step_definitions/stepdefs.rb:12"
                    },
                    "result": {
                    "status": "passed",
                    "duration": 101000
                    }
                },
                {
                    "keyword": "When ",
                    "name": "I ask whether it's Friday yet",
                    "line": 6,
                    "match": {
                    "location": "features/step_definitions/stepdefs.rb:24"
                    },
                    "result": {
                    "status": "passed",
                    "duration": 11000
                    }
                },
                {
                    "keyword": "Then ",
                    "name": "I should be told \"TGIF\"",
                    "line": 7,
                    "match": {
                    "location": "features/step_definitions/stepdefs.rb:28"
                    },
                    "result": {
                    "status": "passed",
                    "duration": 481000
                    }
                }
                ]
            }
        ]
        """
        with open(report_file, 'r') as json_file:
            try:
                data = json.load(json_file)
            except Exception as e:
                raise Exception(
                    "Can't read JSON format report file {}. Make sure to confirm report file."
                    .format(report_file)) from e

        if len(data) == 0:
            click.echo(
                "Can't find test reports from {}. Make sure to confirm report file."
                .format(report_file),
                err=True)

        for d in data:
            file_name = d.get("uri", "")
            class_name = d.get("name", "")
            for element in d.get("elements", []):
                test_case = element.get("name", "")
                steps = {}
                duration = 0
                statuses = []
                stderr = []
                for step in element.get("steps", []):
                    steps[step.get("keyword",
                                   "").strip()] = step.get("name", "").strip()
                    result = step.get("result", None)

                    if result:
                        # duration's unit is nano sec
                        # ref: https://github.com/cucumber/cucumber-ruby/blob/main/lib/cucumber/formatter/json.rb#L222
                        duration = duration + result.get("duration", 0)
                        statuses.append(result.get("status"))
                        if result.get("error_message", None):
                            stderr.append(result["error_message"])

                if "failed" in statuses:
                    status = CaseEvent.TEST_FAILED
                elif "undefined" in statuses:
                    status = CaseEvent.TEST_SKIPPED
                else:
                    status = CaseEvent.TEST_PASSED

                test_path = [
                    {
                        "type":
                        "file",
                        "name":
                        pathlib.Path(
                            self.file_path_normalizer.relativize(
                                file_name)).as_posix()
                    },
                    {
                        "type": "class",
                        "name": class_name
                    },
                    {
                        "type": "testcase",
                        "name": test_case
                    },
                ]

                for k in steps:
                    test_path.append({"type": k, "name": steps[k]})

                yield CaseEvent.create(test_path,
                                       duration / 1000 / 1000 / 1000, status,
                                       None, "\n".join(stderr), None, None)