xref: /unit/test/unit/applications/lang/node.py (revision 1876:f85b85094541)
1import shutil
2from urllib.parse import quote
3
4from unit.applications.proto import TestApplicationProto
5from unit.option import option
6from unit.utils import public_dir
7
8
9class TestApplicationNode(TestApplicationProto):
10    application_type = "node"
11    es_modules = False
12
13    def prepare_env(self, script):
14        # copy application
15        shutil.copytree(
16            option.test_dir + '/node/' + script, option.temp_dir + '/node'
17        )
18
19        # copy modules
20        shutil.copytree(
21            option.current_dir + '/node/node_modules',
22            option.temp_dir + '/node/node_modules',
23        )
24
25        public_dir(option.temp_dir + '/node')
26
27    def load(self, script, name='app.js', **kwargs):
28        self.prepare_env(script)
29
30        if self.es_modules:
31            arguments = [
32                "node",
33                "--loader",
34                "unit-http/loader.mjs",
35                "--require",
36                "unit-http/loader",
37                name,
38            ]
39
40        else:
41            arguments = ["node", "--require", "unit-http/loader", name]
42
43        self._load_conf(
44            {
45                "listeners": {
46                    "*:7080": {"pass": "applications/" + quote(script, '')}
47                },
48                "applications": {
49                    script: {
50                        "type": "external",
51                        "processes": {"spare": 0},
52                        "working_directory": option.temp_dir + '/node',
53                        "executable": '/usr/bin/env',
54                        "arguments": arguments,
55                    }
56                },
57            },
58            **kwargs
59        )
60