Ejemplo n.º 1
0
class Pyp5jsCompilerTests(TestCase):
    def setUp(self):
        self.pyp5js_files = LibFiles()
        self.files = SketchFiles('foo')
        self.compiler = Pyp5jsCompiler(self.files)

        self.files.create_sketch_dir()
        self.files.sketch_py.touch()

    def tearDown(self):
        if SKETCHBOOK_DIR.exists():
            shutil.rmtree(SKETCHBOOK_DIR)

    def test_transcrypt_target_dir_path(self):
        assert self.files.sketch_dir.joinpath(
            '__target__') == self.compiler.target_dir

    def test_command_line_string(self):
        pyp5_dir = self.pyp5js_files.install

        expected = ' '.join([
            str(c) for c in [
                'transcrypt', '-xp', pyp5_dir, '-k', '-ks', '-b', '-m', '-n',
                self.files.target_sketch
            ]
        ])

        assert expected == self.compiler.command_line

    @patch('pyp5js.compiler.subprocess.Popen')
    def test_run_compiler_as_expected(self, MockedPopen):
        proc = Mock(spec=Popen)
        MockedPopen.return_value = proc

        self.compiler.run_compiler()
        expected_command_line = shlex.split(self.compiler.command_line)

        MockedPopen.assert_called_once_with(expected_command_line)
        proc.wait.assert_called_once_with()

    def test_clean_up(self):
        self.compiler.target_dir.mkdir()
        self.files.target_sketch.touch()

        self.compiler.clean_up()

        assert not self.compiler.target_dir.exists()
        assert self.files.target_dir.exists()
        assert not self.files.target_sketch.exists()

    def test_prepare_sketch(self):
        expected_content = get_target_sketch_content(self.files)
        assert not self.files.target_sketch.exists()

        self.compiler.prepare()

        assert self.files.target_sketch.exists()
        with self.files.target_sketch.open('r') as fd:
            content = fd.read()
        assert expected_content == content
Ejemplo n.º 2
0
def new_sketch(sketch_name):
    """
    Creates a new sketch with the required assets and a index.html file, based on pyp5js's templates

    :param sketch_name: name for new sketch
    :type sketch_name: string
    :return: file names
    :rtype: list of strings
    """
    sketch_files = SketchFiles(sketch_name)
    sketch_files.create_sketch_dir()

    templates_files = [
        (sketch_files.from_lib.base_sketch, sketch_files.sketch_py),
        (sketch_files.from_lib.p5js, sketch_files.p5js),
        (sketch_files.from_lib.p5_dom_js, sketch_files.p5_dom_js),
    ]
    for src, dest in templates_files:
        shutil.copyfile(src, dest)

    index_contet = get_sketch_index_content(sketch_files)
    with open(sketch_files.index_html, "w") as fd:
        fd.write(index_contet)

    return sketch_files
Ejemplo n.º 3
0
class SketchFilesTests(TestCase):

    def setUp(self):
        self.base_dir = SKETCHBOOK_DIR
        self.sketch_name = 'foo'
        self.files = SketchFiles(self.sketch_name)

    def tearDown(self):
        if self.base_dir.exists():
            shutil.rmtree(self.base_dir)

    def test_sketch_dirs(self):
        assert self.base_dir.joinpath(self.sketch_name) == self.files.sketch_dir
        assert self.base_dir.joinpath(self.sketch_name, 'static') == self.files.static_dir
        assert self.base_dir.joinpath(self.sketch_name, 'target') == self.files.target_dir
        assert self.files.TARGET_NAME == 'target'

    def test_sketch_files(self):
        self.files.check_sketch_dir = False
        assert self.base_dir.joinpath(self.sketch_name, 'index.html') == self.files.index_html
        assert self.base_dir.joinpath(self.sketch_name, 'static', 'p5.js') == self.files.p5js
        assert self.base_dir.joinpath(self.sketch_name, 'static', 'p5.dom.js') == self.files.p5_dom_js
        assert self.base_dir.joinpath(self.sketch_name, 'foo.py') == self.files.sketch_py
        assert self.base_dir.joinpath(self.sketch_name, 'target_sketch.py') == self.files.target_sketch

    def test_sketch_files_holds_reference_to_lib_files(self):
        lib_files = LibFiles()
        assert isinstance(self.files.from_lib, LibFiles)
        assert self.files.from_lib.install == lib_files.install

    def test_create_dirs(self):
        assert self.files.sketch_dir.exists() is False
        assert self.files.static_dir.exists() is False
        assert self.files.target_dir.exists() is False

        self.files.create_sketch_dir()

        assert self.files.sketch_dir.exists() is True
        assert self.files.static_dir.exists() is True
        assert self.files.target_dir.exists() is True

        with pytest.raises(SketchDirAlreadyExistException):
            self.files.create_sketch_dir()

    def test_raise_exception_when_name_starts_with_numbers(self):
        with pytest.raises(InvalidName):
            SketchFiles('123name')

    def test_raise_exception_when_name_contains_non_alphanumeric_chars(self):
        with pytest.raises(InvalidName):
            SketchFiles('name&')

    def test_name_should_accept_underscore_in_the_beginning(self):
        file = SketchFiles('__name__')
        assert file.sketch_name == '__name__'

    def test_name_should_accept_underscore_in_the_middle(self):
        file = SketchFiles('na_me')
        assert file.sketch_name == 'na_me'
Ejemplo n.º 4
0
class SketchFilesTests(TestCase):
    def setUp(self):
        self.base_dir = SKETCHBOOK_DIR
        self.sketch_name = 'foo'
        self.files = SketchFiles(self.sketch_name)

    def tearDown(self):
        if self.base_dir.exists():
            shutil.rmtree(self.base_dir)

    def test_sketch_dirs(self):
        assert self.base_dir.joinpath(
            self.sketch_name) == self.files.sketch_dir
        assert self.base_dir.joinpath(self.sketch_name,
                                      'static') == self.files.static_dir
        assert self.base_dir.joinpath(self.sketch_name,
                                      'target') == self.files.target_dir
        assert self.files.TARGET_NAME == 'target'

    def test_sketch_files(self):
        self.files.check_sketch_dir = False
        assert self.base_dir.joinpath(self.sketch_name,
                                      'index.html') == self.files.index_html
        assert self.base_dir.joinpath(self.sketch_name, 'static',
                                      'p5.js') == self.files.p5js
        assert self.base_dir.joinpath(self.sketch_name, 'static',
                                      'p5.dom.js') == self.files.p5_dom_js
        assert self.base_dir.joinpath(self.sketch_name,
                                      'foo.py') == self.files.sketch_py
        assert self.base_dir.joinpath(
            self.sketch_name, 'target_sketch.py') == self.files.target_sketch

    def test_sketch_files_holds_reference_to_lib_files(self):
        lib_files = LibFiles()
        assert isinstance(self.files.from_lib, LibFiles)
        assert self.files.from_lib.install == lib_files.install

    def test_create_dirs(self):
        assert self.files.sketch_dir.exists() is False
        assert self.files.static_dir.exists() is False
        assert self.files.target_dir.exists() is False

        self.files.create_sketch_dir()

        assert self.files.sketch_dir.exists() is True
        assert self.files.static_dir.exists() is True
        assert self.files.target_dir.exists() is True

        with pytest.raises(SketchDirAlreadyExistException):
            self.files.create_sketch_dir()
Ejemplo n.º 5
0
class TestNewSketchCommand(TestCase):
    def setUp(self):
        self.sketch_name = 'foo'
        self.sketch_files = SketchFiles(self.sketch_name)

    def tearDown(self):
        if SKETCHBOOK_DIR.exists():
            shutil.rmtree(SKETCHBOOK_DIR)

    def test_create_new_sketch_with_all_required_files(self):
        commands.new_sketch(self.sketch_name)

        assert self.sketch_files.index_html.exists()
        assert self.sketch_files.sketch_py.exists()
        assert self.sketch_files.p5js.exists()
        assert self.sketch_files.p5_dom_js.exists()

    def test_raise_exception_if_dir_already_exist(self):
        self.sketch_files.create_sketch_dir()

        with pytest.raises(SketchDirAlreadyExistException):
            commands.new_sketch(self.sketch_name)
Ejemplo n.º 6
0
def files():
    files = SketchFiles('foo')
    files.create_sketch_dir()
    yield files
    shutil.rmtree(SKETCHBOOK_DIR)
Ejemplo n.º 7
0
 def test_raise_exception_when_name_creating_dir_with_invalid_name(self):
     files = SketchFiles('name&')
     with pytest.raises(InvalidName):
         files.create_sketch_dir()