Skip to content

Commit

Permalink
run pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yagebu committed Jan 9, 2022
1 parent a3e0366 commit 689edf4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 86 deletions.
41 changes: 20 additions & 21 deletions lektor_webpack_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,60 @@

from lektor.pluginsystem import Plugin
from lektor.reporter import reporter
from lektor.utils import locate_executable, portable_popen
from lektor.utils import locate_executable
from lektor.utils import portable_popen


class WebpackSupportPlugin(Plugin):
name = 'Webpack Support Plugin'
description = 'Super simple Lektor plugin that runs a webpack watcher'
name = "Webpack Support Plugin"
description = "Super simple Lektor plugin that runs a webpack watcher"

def __init__(self, *args, **kwargs):
Plugin.__init__(self, *args, **kwargs)
self.webpack_process = None

def is_enabled(self, extra_flags):
return bool(extra_flags.get('webpack'))
return bool(extra_flags.get("webpack"))

def run_webpack(self, watch=False):
webpack_root = os.path.join(self.env.root_path, 'webpack')
args = [os.path.join(webpack_root, 'node_modules', '.bin', 'webpack')]
webpack_root = os.path.join(self.env.root_path, "webpack")
args = [os.path.join(webpack_root, "node_modules", ".bin", "webpack")]
if watch:
args.append('--watch')
args.append("--watch")
return portable_popen(args, cwd=webpack_root)

def install_node_dependencies(self):
webpack_root = os.path.join(self.env.root_path, 'webpack')
webpack_root = os.path.join(self.env.root_path, "webpack")

# Use yarn over npm if it's availabe and there is a yarn lockfile
has_yarn_lockfile = os.path.exists(os.path.join(
webpack_root, 'yarn.lock'))
pkg_manager = 'npm'
if locate_executable('yarn') is not None and has_yarn_lockfile:
pkg_manager = 'yarn'
has_yarn_lockfile = os.path.exists(os.path.join(webpack_root, "yarn.lock"))
pkg_manager = "npm"
if locate_executable("yarn") is not None and has_yarn_lockfile:
pkg_manager = "yarn"

reporter.report_generic('Running {} install'.format(pkg_manager))
portable_popen([pkg_manager, 'install'], cwd=webpack_root).wait()
reporter.report_generic("Running {} install".format(pkg_manager))
portable_popen([pkg_manager, "install"], cwd=webpack_root).wait()

def on_server_spawn(self, **extra):
extra_flags = extra.get("extra_flags") or extra.get("build_flags") or {}
if not self.is_enabled(extra_flags):
return
self.install_node_dependencies()
reporter.report_generic('Spawning webpack watcher')
reporter.report_generic("Spawning webpack watcher")
self.webpack_process = self.run_webpack(watch=True)

def on_server_stop(self, **extra):
if self.webpack_process is not None:
reporter.report_generic('Stopping webpack watcher')
reporter.report_generic("Stopping webpack watcher")
self.webpack_process.kill()

def on_before_build_all(self, builder, **extra):
extra_flags = getattr(
builder, "extra_flags", getattr(builder, "build_flags", None)
)
if not self.is_enabled(extra_flags) \
or self.webpack_process is not None:
if not self.is_enabled(extra_flags) or self.webpack_process is not None:
return
self.install_node_dependencies()
reporter.report_generic('Starting webpack build')
reporter.report_generic("Starting webpack build")
self.run_webpack().wait()
reporter.report_generic('Webpack build finished')
reporter.report_generic("Webpack build finished")
55 changes: 28 additions & 27 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,47 @@

from setuptools import setup

with io.open('README.md', 'rt', encoding="utf8") as f:
with io.open("README.md", "rt", encoding="utf8") as f:
readme = f.read()

_description_re = re.compile(r'description\s+=\s+(?P<description>.*)')
_description_re = re.compile(r"description\s+=\s+(?P<description>.*)")

with open('lektor_webpack_support.py', 'rb') as f:
description = str(ast.literal_eval(_description_re.search(
f.read().decode('utf-8')).group(1)))
with open("lektor_webpack_support.py", "rb") as f:
description = str(
ast.literal_eval(_description_re.search(f.read().decode("utf-8")).group(1))
)

tests_require = [
'lektor',
'pytest',
'pytest-cov',
'pytest-mock',
"lektor",
"pytest",
"pytest-cov",
"pytest-mock",
]

setup(
author='Armin Ronacher',
author_email='[email protected]',
author="Armin Ronacher",
author_email="[email protected]",
description=description,
keywords = 'Lektor plugin node webpack yarn scss static-site',
extras_require={'test': tests_require},
license='BSD',
keywords="Lektor plugin node webpack yarn scss static-site",
extras_require={"test": tests_require},
license="BSD",
long_description=readme,
long_description_content_type='text/markdown',
py_modules=['lektor_webpack_support'],
long_description_content_type="text/markdown",
py_modules=["lektor_webpack_support"],
tests_require=tests_require,
url='http://github.com/lektor/lektor-webpack-support',
version='0.5',
name='lektor-webpack-support',
url="http://github.com/lektor/lektor-webpack-support",
version="0.5",
name="lektor-webpack-support",
classifiers=[
'Environment :: Plugins',
'Environment :: Web Environment',
'Framework :: Lektor',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
"Environment :: Plugins",
"Environment :: Web Environment",
"Framework :: Lektor",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
],
entry_points={
'lektor.plugins': [
'webpack-support = lektor_webpack_support:WebpackSupportPlugin',
"lektor.plugins": [
"webpack-support = lektor_webpack_support:WebpackSupportPlugin",
]
}
},
)
20 changes: 10 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import os

import pytest
from lektor.builder import Builder
from lektor.db import Database
from lektor.project import Project
from lektor.environment import Environment
from lektor.project import Project

from lektor_webpack_support import WebpackSupportPlugin


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def project():
return Project.from_path(os.path.join(os.path.dirname(__file__),
'demo-project'))
return Project.from_path(os.path.join(os.path.dirname(__file__), "demo-project"))


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def env(project):
return Environment(project)


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def pad(env):
return Database(env).new_pad()


@pytest.fixture(scope='function')
@pytest.fixture(scope="function")
def builder(tmpdir, pad):
output_dir = str(tmpdir.mkdir("output"))
try:
return Builder(pad, output_dir, extra_flags=('webpack',))
return Builder(pad, output_dir, extra_flags=("webpack",))
except TypeError:
return Builder(pad, output_dir, build_flags=('webpack',))

return Builder(pad, output_dir, build_flags=("webpack",))


@pytest.fixture
Expand Down
44 changes: 16 additions & 28 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ def test_basic(plugin, builder, env, mocker):
mock_popen = mocker.patch("lektor_webpack_support.portable_popen")
plugin.on_before_build_all(builder)
env_path = py.path.local(env.root_path)
mock_popen.assert_any_call(["npm", "install"], cwd=env_path / "webpack")
mock_popen.assert_any_call(
["npm", "install"],
cwd=env_path / 'webpack'
)
mock_popen.assert_any_call(
[env_path / 'webpack' / 'node_modules' / '.bin' / 'webpack'],
cwd=env_path / 'webpack'
[env_path / "webpack" / "node_modules" / ".bin" / "webpack"],
cwd=env_path / "webpack",
)
assert plugin.webpack_process is None

Expand All @@ -30,13 +27,10 @@ def test_watcher(plugin, env, mocker):
mock_popen = mocker.patch("lektor_webpack_support.portable_popen")
plugin.on_server_spawn(extra_flags={"webpack": True})
env_path = py.path.local(env.root_path)
mock_popen.assert_any_call(["npm", "install"], cwd=env_path / "webpack")
mock_popen.assert_any_call(
["npm", "install"],
cwd=env_path / 'webpack'
)
mock_popen.assert_any_call(
[env_path / 'webpack' / 'node_modules' / '.bin' / 'webpack', '--watch'],
cwd=env_path / 'webpack'
[env_path / "webpack" / "node_modules" / ".bin" / "webpack", "--watch"],
cwd=env_path / "webpack",
)
assert plugin.webpack_process is mock_popen.return_value

Expand All @@ -45,13 +39,10 @@ def test_watcher_build_flags(plugin, env, mocker):
mock_popen = mocker.patch("lektor_webpack_support.portable_popen")
plugin.on_server_spawn(build_flags={"webpack": True})
env_path = py.path.local(env.root_path)
mock_popen.assert_any_call(["npm", "install"], cwd=env_path / "webpack")
mock_popen.assert_any_call(
["npm", "install"],
cwd=env_path / 'webpack'
)
mock_popen.assert_any_call(
[env_path / 'webpack' / 'node_modules' / '.bin' / 'webpack', '--watch'],
cwd=env_path / 'webpack'
[env_path / "webpack" / "node_modules" / ".bin" / "webpack", "--watch"],
cwd=env_path / "webpack",
)
assert plugin.webpack_process is mock_popen.return_value

Expand All @@ -69,22 +60,19 @@ def test_server_stop(plugin, mocker):


def test_yarn_support(plugin, env, mocker):
mock_popen = mocker.patch('lektor_webpack_support.portable_popen')
mock_popen = mocker.patch("lektor_webpack_support.portable_popen")

mock_path_exists = mocker.patch('os.path.exists')
mock_path_exists = mocker.patch("os.path.exists")
mock_path_exists.return_value = True

mock_locate_executable = mocker.patch('lektor_webpack_support.locate_executable')
mock_locate_executable.return_value = '/usr/local/bin/yarn'
mock_locate_executable = mocker.patch("lektor_webpack_support.locate_executable")
mock_locate_executable.return_value = "/usr/local/bin/yarn"

env_path = py.path.local(env.root_path)
plugin.install_node_dependencies()

assert mock_path_exists.called
assert mock_path_exists.call_args[0][0].endswith('yarn.lock')
assert mock_path_exists.call_args[0][0].endswith("yarn.lock")
assert mock_locate_executable.called
mock_locate_executable.assert_any_call('yarn')
mock_popen.assert_any_call(
['yarn', 'install'],
cwd=env_path / 'webpack'
)
mock_locate_executable.assert_any_call("yarn")
mock_popen.assert_any_call(["yarn", "install"], cwd=env_path / "webpack")

0 comments on commit 689edf4

Please sign in to comment.