Esempio n. 1
0
    def setUp(self, OSUtilMock):

        self.osutils = OSUtilMock.return_value
        self.osutils.pipe = 'PIPE'
        self.popen = FakePopen()
        self.osutils.popen.side_effect = [self.popen]
        self.under_test = SubprocessNpm(self.osutils, npm_exe="/a/b/c/npm.exe")
    def test_builds_project_with_remote_dependencies_without_download_dependencies_with_dependencies_dir(
            self, runtime):
        source_dir = os.path.join(self.TEST_DATA_FOLDER,
                                  "with-deps-no-node_modules")
        options = {"entry_points": ["included.js"]}

        osutils = OSUtils()
        npm = SubprocessNpm(osutils)
        esbuild_dir = os.path.join(self.TEST_DATA_FOLDER, "esbuild-binary")
        npm.run(["ci"], cwd=esbuild_dir)
        binpath = npm.run(["bin"], cwd=esbuild_dir)

        self.builder.build(
            source_dir,
            self.artifacts_dir,
            self.scratch_dir,
            os.path.join(source_dir, "package.json"),
            options=options,
            runtime=runtime,
            dependencies_dir=self.dependencies_dir,
            download_dependencies=False,
            experimental_flags=[EXPERIMENTAL_FLAG_ESBUILD],
            executable_search_paths=[binpath],
        )

        expected_files = {"included.js.map", "included.js"}
        output_files = set(os.listdir(self.artifacts_dir))
        self.assertEqual(expected_files, output_files)
    def test_builds_with_external_esbuild(self, runtime):
        osutils = OSUtils()
        npm = SubprocessNpm(osutils)
        source_dir = os.path.join(self.TEST_DATA_FOLDER, "no-deps-esbuild")
        esbuild_dir = os.path.join(self.TEST_DATA_FOLDER, "esbuild-binary")

        npm.run(["ci"], cwd=esbuild_dir)

        binpath = npm.run(["bin"], cwd=esbuild_dir)

        options = {"entry_points": ["included.js"]}

        self.builder.build(
            source_dir,
            self.artifacts_dir,
            self.scratch_dir,
            os.path.join(source_dir, "package.json"),
            runtime=runtime,
            options=options,
            executable_search_paths=[binpath],
            experimental_flags=[EXPERIMENTAL_FLAG_ESBUILD],
        )

        expected_files = {"included.js", "included.js.map"}
        output_files = set(os.listdir(self.artifacts_dir))
        self.assertEqual(expected_files, output_files)
Esempio n. 4
0
    def test_run_executes_npm_cmd_on_windows(self):
        self.osutils.is_windows.side_effect = [True]

        self.under_test = SubprocessNpm(self.osutils)

        self.under_test.run(["pack", "-q"])

        self.osutils.popen.assert_called_with(["npm.cmd", "pack", "-q"],
                                              cwd=None,
                                              stderr="PIPE",
                                              stdout="PIPE")
Esempio n. 5
0
    def test_run_executes_npm_cmd_on_windows(self):

        self.osutils.is_windows.side_effect = [True]

        self.under_test = SubprocessNpm(self.osutils)

        self.under_test.run(['pack', '-q'])

        self.osutils.popen.assert_called_with(['npm.cmd', 'pack', '-q'],
                                              cwd=None,
                                              stderr='PIPE',
                                              stdout='PIPE')
Esempio n. 6
0
class TestSubprocessNpm(TestCase):
    @patch("aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils")
    def setUp(self, OSUtilMock):
        self.osutils = OSUtilMock.return_value
        self.osutils.pipe = "PIPE"
        self.popen = FakePopen()
        self.osutils.popen.side_effect = [self.popen]
        self.under_test = SubprocessNpm(self.osutils, npm_exe="/a/b/c/npm.exe")

    def test_run_executes_npm_on_nixes(self):
        self.osutils.is_windows.side_effect = [False]

        self.under_test = SubprocessNpm(self.osutils)

        self.under_test.run(["pack", "-q"])

        self.osutils.popen.assert_called_with(["npm", "pack", "-q"],
                                              cwd=None,
                                              stderr="PIPE",
                                              stdout="PIPE")

    def test_run_executes_npm_cmd_on_windows(self):
        self.osutils.is_windows.side_effect = [True]

        self.under_test = SubprocessNpm(self.osutils)

        self.under_test.run(["pack", "-q"])

        self.osutils.popen.assert_called_with(["npm.cmd", "pack", "-q"],
                                              cwd=None,
                                              stderr="PIPE",
                                              stdout="PIPE")

    def test_uses_custom_npm_path_if_supplied(self):
        self.under_test.run(["pack", "-q"])

        self.osutils.popen.assert_called_with(["/a/b/c/npm.exe", "pack", "-q"],
                                              cwd=None,
                                              stderr="PIPE",
                                              stdout="PIPE")

    def test_uses_cwd_if_supplied(self):
        self.under_test.run(["pack", "-q"], cwd="/a/cwd")

        self.osutils.popen.assert_called_with(["/a/b/c/npm.exe", "pack", "-q"],
                                              cwd="/a/cwd",
                                              stderr="PIPE",
                                              stdout="PIPE")

    def test_returns_popen_out_decoded_if_retcode_is_0(self):
        self.popen.out = b"some encoded text\n\n"

        result = self.under_test.run(["pack"])

        self.assertEqual(result, "some encoded text")

    def test_raises_NpmExecutionError_with_err_text_if_retcode_is_not_0(self):
        self.popen.returncode = 1
        self.popen.err = b"some error text\n\n"

        with self.assertRaises(NpmExecutionError) as raised:
            self.under_test.run(["pack"])

        self.assertEqual(raised.exception.args[0],
                         "NPM Failed: some error text")

    def test_raises_ValueError_if_args_not_a_list(self):
        with self.assertRaises(ValueError) as raised:
            self.under_test.run(("pack"))

        self.assertEqual(raised.exception.args[0], "args must be a list")

    def test_raises_ValueError_if_args_empty(self):
        with self.assertRaises(ValueError) as raised:
            self.under_test.run([])

        self.assertEqual(raised.exception.args[0], "requires at least one arg")