Best Python code snippet using Airtest
test_translator_drone.py
Source:test_translator_drone.py  
...235                "env": {"foo": "variable 1", "var": "variable 2",},236            }237        )238        wf_env = {"baz": "variable 3"}239        drone_step = dt._translate_step(popper_step, "docker", wf_env)240        self.assertEqual(241            drone_step,242            Box(243                {244                    "name": "download",245                    "image": "byrnedo/alpine-curl:0.1.8",246                    "entrypoint": ["curl"],247                    "command": [248                        "-LO",249                        "https://github.com/datasets/co2-fossil-global/raw/master/global.csv",250                    ],251                    "environment": {252                        "foo": "variable 1",253                        "var": "variable 2",254                        "baz": "variable 3",255                    },256                }257            ),258        )259    def test_translate_step_dir(self):260        dt = DroneTranslator()261        popper_step = Box(262            {263                "id": "download",264                "uses": "docker://alpine",265                "dir": "/tmp",266                "runs": ["touch"],267                "args": ["file1", "file2"],268            }269        )270        drone_step = dt._translate_step(popper_step, "docker", {})271        self.assertEqual(272            drone_step,273            Box(274                {275                    "name": "download",276                    "image": "alpine",277                    "commands": [278                        "ln -s /drone/src /workspace",279                        "cd /tmp",280                        "touch file1 file2",281                    ],282                    "environment": {},283                }284            ),285        )286        popper_step_dir_without_runs = Box(287            {288                "id": "download",289                "uses": "docker://alpine",290                "dir": "/tmp",291                "args": ["echo", "missing runs"],292            }293        )294        with self.assertRaises(AttributeError):295            # raise an error if `runs` is empty296            dt._translate_step(popper_step_dir_without_runs, "docker", {})297    def test_translate_step_exec(self):298        dt = DroneTranslator()299        popper_step = Box(300            {301                "id": "download",302                "uses": "sh",303                "runs": ["curl"],304                "args": [305                    "-LO",306                    "https://github.com/datasets/co2-fossil-global/raw/master/global.csv",307                ],308                "env": {"foo": "variable 1", "var": "variable 2",},309            }310        )311        wf_env = {"baz": "variable 3"}312        drone_step = dt._translate_step(popper_step, "exec", wf_env)313        self.assertEqual(314            drone_step,315            Box(316                {317                    "name": "download",318                    "commands": [319                        "curl -LO https://github.com/datasets/co2-fossil-global/raw/master/global.csv"320                    ],321                    "environment": {322                        "foo": "variable 1",323                        "var": "variable 2",324                        "baz": "variable 3",325                    },326                }327            ),328        )329        popper_step_empty_runs = Box(330            {331                "id": "1",332                "uses": "sh",333                "runs": [],  # parser will create an empty array if `runs` is not specified334            }335        )336        with self.assertRaises(AttributeError):337            # raise an error if `runs` is empty338            dt._translate_step(popper_step_empty_runs, "exec", wf_env)339    def test_translate_step_optional(self):340        dt = DroneTranslator()341        # only "uses" attribute is required in Popper342        popper_step = Box(343            {344                "id": "1",  # this is optional but the Popper parser assigns a sequential id345                "uses": "docker://byrnedo/alpine-curl:0.1.8",346            }347        )348        drone_step = dt._translate_step(popper_step, "docker", {"foo": "var1"})349        self.assertEqual(350            drone_step,351            Box(352                {353                    "name": "1",354                    "image": "byrnedo/alpine-curl:0.1.8",355                    "environment": {"foo": "var1"},356                }357            ),358        )359    def test_uses_non_docker(self):360        dt = DroneTranslator()361        with self.assertRaises(AttributeError):362            dt._translate_uses("./path/to/myimg/")...translator_task.py
Source:translator_task.py  
...38            if step_id == "default":39                raise AttributeError(40                    f"'default' cannot be used as a step ID when translating Popper to Task."41                )42            box["tasks"][step["id"]] = self._translate_step(step, box["env"])43        # call steps in order from default task44        box["tasks"]["default"] = {45            "cmds": [{"task": step["id"]} for step in wf["steps"]]46        }47        return box.to_yaml()48    # translate a step49    def _translate_step(self, step, env):50        t = self._detect_type(step["uses"])51        if t == "docker":52            return self._translate_docker_step(step, env)53        elif t == "sh":54            return self._translate_sh_step(step)55    # detect step type (docker or exec)56    def _detect_type(self, uses):57        if "docker://" in uses:58            return "docker"59        if uses == "sh":60            return "sh"61        raise AttributeError(f"Unexpected value {uses} found in `uses` attribute")62    # translate Popper steps to be executed on host63    def _translate_sh_step(self, step):...translator_drone.py
Source:translator_drone.py  
...19        if "options" in wf:20            if "env" in wf["options"]:21                wf_env = {**wf["options"]["env"], **wf_env}22        box["steps"] = [23            self._translate_step(step, wf_type, wf_env) for step in wf["steps"]24        ]25        return box.to_yaml()26    # given a popper workflow, detect the corresponding Drone's pipeline type27    def _detect_type(self, wf):28        if not wf["steps"]:29            raise AttributeError("`steps` must not be empty")30        # helper function that detects the type from Popper's `uses` field31        def detect_type(uses):32            if "docker://" in uses:33                return "docker"34            if uses == "sh":35                return "exec"36            raise AttributeError(f"Unexpected value {uses} found in `uses` attribute")37        # determine the types of each Popper step38        ts = [detect_type(step["uses"]) for step in wf["steps"]]39        # make sure all types are the same40        for t in ts[1:]:41            if ts[0] != t:42                raise AttributeError(43                    "Drone supports only one runner type per pipeline. Popper workflows that use both `sh` and Docker images cannot be translated."44                )45        return ts[0]46    def _translate_step(self, popper_step, wf_type, wf_env):47        drone_step = Box()48        drone_step["name"] = popper_step["id"]49        if wf_type == "docker":50            # set docker image51            drone_step["image"] = self._translate_uses(popper_step["uses"])52            # handle `dir` option53            # only with docker pipeline as `popper exec` does not respect this option with `uses: sh`54            if "dir" in popper_step:55                if not popper_step["runs"]:56                    raise AttributeError(57                        "Workflow with `dir` must specify `runs` to translation"58                    )59                drone_step["commands"] = [60                    # create a symbolic link to create the same the directry structure in Drone...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
