예제 #1
0
 def test_includes_multiple_entry_points_if_requested(self):
     action = EsbuildBundleAction(
         "source",
         "artifacts",
         {
             "entry_points": ["x.js", "y.js"],
             "target": "node14"
         },
         self.osutils,
         self.subprocess_esbuild,
     )
     action.execute()
     self.subprocess_esbuild.run.assert_called_with(
         [
             "x.js",
             "y.js",
             "--bundle",
             "--platform=node",
             "--format=cjs",
             "--minify",
             "--sourcemap",
             "--target=node14",
             "--outdir=artifacts",
         ],
         cwd="source",
     )
예제 #2
0
 def test_packages_with_externals(self):
     action = EsbuildBundleAction(
         "source",
         "artifacts",
         {
             "entry_points": ["x.js"],
             "external": ["fetch", "aws-sdk"]
         },
         self.osutils,
         self.subprocess_esbuild,
     )
     action.execute()
     self.subprocess_esbuild.run.assert_called_with(
         [
             "x.js",
             "--bundle",
             "--platform=node",
             "--format=cjs",
             "--minify",
             "--sourcemap",
             "--external:fetch",
             "--external:aws-sdk",
             "--target=es2020",
             "--outdir=artifacts",
         ],
         cwd="source",
     )
예제 #3
0
 def test_packages_with_custom_loaders(self):
     action = EsbuildBundleAction(
         "source",
         "artifacts",
         {
             "entry_points": ["x.js"],
             "loader": [".proto=text", ".json=js"]
         },
         self.osutils,
         self.subprocess_esbuild,
     )
     action.execute()
     self.subprocess_esbuild.run.assert_called_with(
         [
             "x.js",
             "--bundle",
             "--platform=node",
             "--format=cjs",
             "--minify",
             "--sourcemap",
             "--loader:.proto=text",
             "--loader:.json=js",
             "--target=es2020",
             "--outdir=artifacts",
         ],
         cwd="source",
     )
예제 #4
0
    def test_raises_error_if_entrypoints_not_specified(self):
        action = EsbuildBundleAction("source", "artifacts",
                                     {"config": "param"}, self.osutils,
                                     self.subprocess_esbuild)
        with self.assertRaises(ActionFailedError) as raised:
            action.execute()

        self.assertEqual(raised.exception.args[0],
                         "entry_points not set ({'config': 'param'})")
예제 #5
0
 def setUp(self, OSUtilMock, SubprocessEsbuildMock):
     self.osutils = OSUtilMock.return_value
     self.subprocess_esbuild = SubprocessEsbuildMock.return_value
     self.action = EsbuildBundleAction(
         "source",
         "artifacts",
         {},
         self.osutils,
         self.subprocess_esbuild,
     )
예제 #6
0
    def test_raises_error_if_entrypoints_empty_list(self):
        action = EsbuildBundleAction("source", "artifacts", {
            "config": "param",
            "entry_points": []
        }, self.osutils, self.subprocess_esbuild)
        with self.assertRaises(ActionFailedError) as raised:
            action.execute()

        self.assertEqual(
            raised.exception.args[0],
            "entry_points must not be empty ({'config': 'param', 'entry_points': []})"
        )
예제 #7
0
 def test_runs_node_subprocess_if_deps_skipped(self):
     action = EsbuildBundleAction(
         tempfile.mkdtemp(),
         "artifacts",
         {"entry_points": ["app.ts"]},
         self.osutils,
         self.subprocess_esbuild,
         self.subprocess_nodejs,
         True,
     )
     action.execute()
     self.subprocess_nodejs.run.assert_called()
예제 #8
0
    def test_checks_if_single_entrypoint_exists(self):

        action = EsbuildBundleAction("source", "artifacts",
                                     {"entry_points": ["x.js"]}, self.osutils,
                                     self.subprocess_esbuild)
        self.osutils.file_exists.side_effect = [False]

        with self.assertRaises(ActionFailedError) as raised:
            action.execute()

        self.osutils.file_exists.assert_called_with("source/x.js")

        self.assertEqual(raised.exception.args[0],
                         "entry point source/x.js does not exist")
예제 #9
0
    def test_reads_nodejs_bundle_template_file(self):
        template = EsbuildBundleAction._get_node_esbuild_template(["app.ts"],
                                                                  "es2020",
                                                                  "outdir",
                                                                  False, True)
        expected_template = """let skipBundleNodeModules = {
  name: 'make-all-packages-external',
  setup(build) {
    let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ // Must not start with "/" or "./" or "../"
    build.onResolve({ filter }, args => ({ path: args.path, external: true }))
  },
}

require('esbuild').build({
  entryPoints: ['app.ts'],
  bundle: true,
  platform: 'node',
  format: 'cjs',
  target: 'es2020',
  sourcemap: true,
  outdir: 'outdir',
  minify: false,
  plugins: [skipBundleNodeModules],
}).catch(() => process.exit(1))
"""
        self.assertEqual(template, expected_template)
예제 #10
0
 def test_does_not_minify_if_requested(self):
     action = EsbuildBundleAction("source", "artifacts", {
         "entry_points": ["x.js"],
         "minify": False
     }, self.osutils, self.subprocess_esbuild)
     action.execute()
     self.subprocess_esbuild.run.assert_called_with(
         [
             "x.js",
             "--bundle",
             "--platform=node",
             "--format=cjs",
             "--sourcemap",
             "--target=es2020",
             "--outdir=artifacts",
         ],
         cwd="source",
     )
예제 #11
0
    def test_packages_javascript_with_minification_and_sourcemap(self):
        action = EsbuildBundleAction("source", "artifacts",
                                     {"entry_points": ["x.js"]}, self.osutils,
                                     self.subprocess_esbuild)
        action.execute()

        self.subprocess_esbuild.run.assert_called_with(
            [
                "x.js",
                "--bundle",
                "--platform=node",
                "--format=cjs",
                "--minify",
                "--sourcemap",
                "--target=es2020",
                "--outdir=artifacts",
            ],
            cwd="source",
        )
예제 #12
0
 def test_uses_specified_target(self):
     action = EsbuildBundleAction("source", "artifacts", {
         "entry_points": ["x.js"],
         "target": "node14"
     }, self.osutils, self.subprocess_esbuild)
     action.execute()
     self.subprocess_esbuild.run.assert_called_with(
         [
             "x.js",
             "--bundle",
             "--platform=node",
             "--format=cjs",
             "--minify",
             "--sourcemap",
             "--target=node14",
             "--outdir=artifacts",
         ],
         cwd="source",
     )
예제 #13
0
class TestImplicitFileTypeResolution(TestCase):
    @patch("aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils")
    @patch(
        "aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild"
    )
    def setUp(self, OSUtilMock, SubprocessEsbuildMock):
        self.osutils = OSUtilMock.return_value
        self.subprocess_esbuild = SubprocessEsbuildMock.return_value
        self.action = EsbuildBundleAction(
            "source",
            "artifacts",
            {},
            self.osutils,
            self.subprocess_esbuild,
        )

    @parameterized.expand([
        ([True], "file.ts", "file.ts"),
        ([False, True], "file", "file.js"),
        ([True], "file", "file.ts"),
    ])
    def test_implicit_and_explicit_file_types(self, file_exists, entry_point,
                                              expected):
        self.osutils.file_exists.side_effect = file_exists
        explicit_entry_point = self.action._get_explicit_file_type(
            entry_point, "")
        self.assertEqual(expected, explicit_entry_point)

    @parameterized.expand([
        ([False], "file.ts"),
        ([False, False], "file"),
    ])
    def test_throws_exception_entry_point_not_found(self, file_exists,
                                                    entry_point):
        self.osutils.file_exists.side_effect = file_exists
        with self.assertRaises(ActionFailedError) as context:
            self.action._get_explicit_file_type(entry_point, "invalid")
        self.assertEqual(str(context.exception),
                         "entry point invalid does not exist")