Example #1
0
    def test_regular_source(self):
        tarfile = MagicMock()
        zipfile = MagicMock()
        move = MagicMock()
        stdout = MagicMock()

        file = tempfile.NamedTemporaryFile()

        with \
                patch('sys.stdout.buffer.write', stdout), \
                patch('sys.stdout.isatty', lambda: False), patch('sys.stdin.isatty', lambda: False), \
                patch('zipfile.ZipFile', zipfile), patch('tarfile.open', tarfile), \
                patch('shutil.move', move), \
                patch('tempfile.NamedTemporaryFile', MagicMock(return_value=file)):
            run(['testing'])

        move.assert_called_once_with(
            file.name,
            './testing-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.zip'
        )

        tarfile.assert_not_called()
        zipfile.assert_called_once_with(file, 'w')
        stdout.assert_called_once_with(
            b'./testing-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.zip\n'
        )
Example #2
0
    def test_inputs_tty_help(self):
        parser_print_help_mock = MagicMock()

        with \
                patch('sys.stdout.isatty', lambda: True),\
                patch('sys.stdin.isatty', lambda: True), \
                patch('argparse.ArgumentParser.print_help', parser_print_help_mock):
            run('')

        parser_print_help_mock.assert_called_once_with()
Example #3
0
    def test(self):
        stdout = MagicMock()

        with patch('sys.stdout.write', stdout):
            run('--output-dir {} {}'.format(self.tmpdir, self.tmpfile.name).split())

        self.assertEqual(stdout.call_count, 1)

        # The file separator on Windows `\` need to be escaped before used as part of regex comparison,
        # otherwise it will form an incomplete escape character.
        bundle_dir = '{}{}'.format(self.tmpdir, os.sep).replace('\\', '\\\\')

        self.assertRegex(
            stdout.call_args[0][0],
            ('^{}tmp[a-z0-9_]{{6,8}}-[a-f0-9]{{64}}\.zip' + os.linesep + '$').format(bundle_dir)
        )
    def test(self):
        stdout = MagicMock()

        logging_setup.configure_logging(MagicMock(), stdout)

        # Patch configure logging to preserve the output to mock stdout
        configure_logging_mock = MagicMock()
        with patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock):
            run('--output-dir {} {}'.format(self.tmpdir, self.tmpfile.name).split())

        # The file separator on Windows `\` need to be escaped before used as part of regex comparison,
        # otherwise it will form an incomplete escape character.
        bundle_dir = '{}{}'.format(self.tmpdir, os.sep).replace('\\', '\\\\')
        self.assertRegex(
            self.output(stdout),
            'Created digested ZIP archive at {}tmp[a-z0-9_]{{6,8}}-[a-f0-9]{{64}}\.zip'.format(bundle_dir)
        )
Example #5
0
    def test_warn_tar_output_tty(self):
        stdout = MagicMock()
        stderr = MagicMock()
        logging_setup.configure_logging(MagicMock(), stdout, stderr)

        configure_logging_mock = MagicMock()

        with \
                patch('sys.stdout.isatty', lambda: True), \
                patch('sys.stdin.isatty', lambda: True), \
                patch('conductr_cli.logging_setup.configure_logging', configure_logging_mock), \
                patch('sys.exit', lambda _: ()):
            run(['--tar', 'test.tar'])

        self.assertEqual(
            self.output(stderr),
            as_error('Error: shazar: Refusing to write to terminal. Provide -o or redirect elsewhere\n')
        )
Example #6
0
    def test(self):
        stdout = MagicMock()

        logging_setup.configure_logging(MagicMock(), stdout)

        # Patch configure logging to preserve the output to mock stdout
        configure_logging_mock = MagicMock()
        with patch('conductr_cli.logging_setup.configure_logging',
                   configure_logging_mock):
            run('--output-dir {} {}'.format(self.tmpdir,
                                            self.tmpfile.name).split())

        # The file separator on Windows `\` need to be escaped before used as part of regex comparison,
        # otherwise it will form an incomplete escape character.
        bundle_dir = '{}{}'.format(self.tmpdir, os.sep).replace('\\', '\\\\')
        self.assertRegex(
            self.output(stdout),
            'Created digested ZIP archive at {}tmp[a-z0-9_]{{6,8}}-[a-f0-9]{{64}}\.zip'
            .format(bundle_dir))
Example #7
0
    def test_tar_source(self):
        tarfile = MagicMock()
        zipfile = MagicMock()
        stdout = MagicMock()

        file = tempfile.NamedTemporaryFile()

        with \
                patch('sys.stdout.buffer.write', stdout), \
                patch('sys.stdout.isatty', lambda: False),\
                patch('sys.stdin.isatty', lambda: False), \
                patch('zipfile.ZipFile', zipfile),\
                patch('tarfile.open', tarfile), \
                patch('tempfile.NamedTemporaryFile', MagicMock(return_value=file)):
            run(['--tar', 'testing.tar'])

        tarfile.assert_called_once_with('testing.tar', mode='r')
        zipfile.assert_called_once_with(file, 'w')
        stdout.assert_called_once_with(b'\nsha-256/'
                                       b'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
Example #8
0
    def test_output(self):
        mock_tarfile = MagicMock()
        mock_zipfile = MagicMock()
        mock_move = MagicMock()
        mock_open = MagicMock()
        stdout = MagicMock()

        file = tempfile.NamedTemporaryFile()

        with \
                patch('sys.stdout.buffer.write', stdout), \
                patch('sys.stdout.isatty', lambda: False), patch('sys.stdin.isatty', lambda: False), \
                patch('zipfile.ZipFile', mock_zipfile), patch('tarfile.open', mock_tarfile), \
                patch('builtins.open', mock_open), \
                patch('shutil.move', mock_move), \
                patch('tempfile.NamedTemporaryFile', MagicMock(return_value=file)):
            run(['-o', 'test.zip'])

            mock_tarfile.assert_called_once_with(fileobj=sys.stdin.buffer, mode='r|')
            stdout.assert_not_called()
            mock_move.assert_not_called()
            mock_open.assert_called_once_with('test.zip', 'wb')
            mock_zipfile.assert_called_once_with(file, 'w')
Example #9
0
    def test_output_dash(self):
        # tests that providing "-o -" goes through stdout

        tarfile = MagicMock()
        zipfile = MagicMock()
        move = MagicMock()
        stdout = MagicMock()

        file = tempfile.NamedTemporaryFile()

        with \
                patch('sys.stdout.buffer.write', stdout), \
                patch('sys.stdout.isatty', lambda: False), patch('sys.stdin.isatty', lambda: False), \
                patch('zipfile.ZipFile', zipfile), patch('tarfile.open', tarfile), \
                patch('shutil.move', move), \
                patch('tempfile.NamedTemporaryFile', MagicMock(return_value=file)):

            run(['-o', '-'])

            tarfile.assert_called_once_with(fileobj=sys.stdin.buffer, mode='r|')
            move.assert_not_called()
            zipfile.assert_called_once_with(file, 'w')
            stdout.assert_called_once_with(b'\nsha-256/'
                                           b'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
Example #10
0
def main_method():
    from conductr_cli import shazar_main
    shazar_main.run()