# Path: webpack/compiler.py
# def webpack(config_file, context=None, settings=None, manifest=manifest_reader, compiler=build_server):
#     use_manifest = conf.settings.USE_MANIFEST
# 
#     # Allow the USE_MANIFEST setting to be overridden when populating the manifest
#     if settings:
#         use_manifest = settings.get('USE_MANIFEST', use_manifest)
# 
#     if use_manifest:
#         return manifest.read(config_file, context)
# 
#     return compiler.build(
#         config_file=config_file,
#         extra_context=context,
#         setting_overrides=settings,
#     )
# 
# Path: tests/settings.py
# BUNDLES = os.path.join(BASE_DIR, 'bundles',)
# 
# Path: tests/utils.py
# def write_file(file_name, content):
#     with open(file_name, 'w') as _file:
#         _file.write(content)
# 
# def read_file(file_name):
#     with open(file_name, 'r') as _file:
#         return _file.read()
# 
# def clean_output_root():
#     # Clean out any files generated from previous test runs
#     if os.path.exists(settings.OUTPUT_ROOT):
#         shutil.rmtree(settings.OUTPUT_ROOT)


import os
import unittest
import time
from webpack.compiler import webpack
from .settings import BUNDLES
from .utils import write_file, read_file, clean_output_root
WATCHED_SOURCE_CONTENT = """module.exports = '__DJANGO_WEBPACK_WATCH_SOURCE_ONE__';"""


# Ensure that the files are in the state that we expect
write_file(PATH_TO_WATCHED_SOURCE_ENTRY, WATCHED_SOURCE_CONTENT)


class TestWatching(unittest.TestCase):
    _PRODUCTION = True

    @classmethod
    def setUpClass(cls):
        clean_output_root()

    @classmethod
    def tearDownClass(cls):
        clean_output_root()

    def test_source_files_can_be_watched_to_rebuild_a_bundle(self):
        self.assertEqual(read_file(PATH_TO_WATCHED_SOURCE_ENTRY), WATCHED_SOURCE_CONTENT)

        bundle = webpack(PATH_TO_WATCHED_SOURCE_CONFIG, settings={'WATCH': True})

        assets = bundle.get_assets()
        self.assertTrue(len(assets), 1)
        contents = read_file(assets[0])
        self.assertIn('__DJANGO_WEBPACK_WATCH_SOURCE_ONE__', contents)
        self.assertNotIn('__DJANGO_WEBPACK_WATCH_SOURCE_TWO__', contents)

        time.sleep(WATCH_WAIT)

# Path: webpack/compiler.py
# def webpack(config_file, context=None, settings=None, manifest=manifest_reader, compiler=build_server):
#     use_manifest = conf.settings.USE_MANIFEST
# 
#     # Allow the USE_MANIFEST setting to be overridden when populating the manifest
#     if settings:
#         use_manifest = settings.get('USE_MANIFEST', use_manifest)
# 
#     if use_manifest:
#         return manifest.read(config_file, context)
# 
#     return compiler.build(
#         config_file=config_file,
#         extra_context=context,
#         setting_overrides=settings,
#     )
# 
# Path: tests/settings.py
# BUNDLES = os.path.join(BASE_DIR, 'bundles',)
# 
# Path: tests/utils.py
# def write_file(file_name, content):
#     with open(file_name, 'w') as _file:
#         _file.write(content)
# 
# def read_file(file_name):
#     with open(file_name, 'r') as _file:
#         return _file.read()
# 
# def clean_output_root():
#     # Clean out any files generated from previous test runs
#     if os.path.exists(settings.OUTPUT_ROOT):
#         shutil.rmtree(settings.OUTPUT_ROOT)


# Path: tests/test_watching.py
import os
import unittest
import time
from webpack.compiler import webpack
from .settings import BUNDLES
from .utils import write_file, read_file, clean_output_root

WATCHED_SOURCE_CONTENT = """module.exports = '__DJANGO_WEBPACK_WATCH_SOURCE_ONE__';"""


# Ensure that the files are in the state that we expect
write_file(PATH_TO_WATCHED_SOURCE_ENTRY, WATCHED_SOURCE_CONTENT)


class TestWatching(unittest.TestCase):
    _PRODUCTION = True

    @classmethod
    def setUpClass(cls):
        clean_output_root()

    @classmethod
    def tearDownClass(cls):
        clean_output_root()

    def test_source_files_can_be_watched_to_rebuild_a_bundle(self):
        self.assertEqual(read_file(PATH_TO_WATCHED_SOURCE_ENTRY), WATCHED_SOURCE_CONTENT)

        bundle = webpack(PATH_TO_WATCHED_SOURCE_CONFIG, settings={'WATCH': True})

        assets = bundle.get_assets()
        self.assertTrue(len(assets), 1)
        contents = read_file(assets[0])
        self.assertIn('__DJANGO_WEBPACK_WATCH_SOURCE_ONE__', contents)
        self.assertNotIn('__DJANGO_WEBPACK_WATCH_SOURCE_TWO__', contents)

        time.sleep(WATCH_WAIT)

        write_file(PATH_TO_WATCHED_SOURCE_ENTRY, """module.exports = '__DJANGO_WEBPACK_WATCH_SOURCE_TWO__';""")