class CommandsParseRuleChangesTests(XMLBuilderMixin, TestCase):
    def setUp(self):
        super(CommandsParseRuleChangesTests, self).setUp()
        self.cli = CliRunner()
        with self.tree.builder("ROOT") as root:
            root.PRTPAGE(P="1234")
        self.notice_xml = NoticeXML(self.tree.render_xml())

    def test_missing_notice(self):
        """If the necessary notice XML is not present, we should expect a
        dependency error"""
        with self.cli.isolated_filesystem():
            result = self.cli.invoke(parse_rule_changes, ['1111'])
            self.assertTrue(isinstance(result.exception,
                                       dependency.Missing))

    @patch('regparser.commands.parse_rule_changes.process_amendments')
    def test_writes(self, process_amendments):
        """If the notice XML is present, we write the parsed version to disk,
        even if that version's already present"""
        process_amendments.return_value = {'filled': 'values'}
        with self.cli.isolated_filesystem():
            entry.Notice('1111').write(self.notice_xml)
            self.cli.invoke(parse_rule_changes, ['1111'])
            self.assertTrue(process_amendments.called)
            args = process_amendments.call_args[0]
            self.assertTrue(isinstance(args[0], dict))
            self.assertTrue(isinstance(args[1], etree._Element))

            process_amendments.reset_mock()
            entry.Entry('rule_changes', '1111').write('content')
            self.cli.invoke(parse_rule_changes, ['1111'])
            self.assertTrue(process_amendments.called)
Ejemplo n.º 2
0
def test_invalid_url_for_login(monkeypatch, tmpdir):
    runner = CliRunner()
    response = MagicMock()

    monkeypatch.setattr('stups_cli.config.load_config', lambda x: {})
    monkeypatch.setattr('pierone.api.get_token', MagicMock(return_value='tok123'))
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))

    # Missing Pier One header
    response.text = 'Not valid API'
    monkeypatch.setattr('requests.get', lambda *args, **kw: response)

    with runner.isolated_filesystem():
        result = runner.invoke(cli, ['login'], catch_exceptions=False, input='pieroneurl\n')
        assert 'ERROR: Did not find a valid Pier One registry at https://pieroneurl' in result.output
        assert result.exit_code == 1
        assert not os.path.exists(os.path.join(str(tmpdir), '.docker/config.json'))

    # Not a valid header
    response.raise_for_status = MagicMock(side_effect=RequestException)
    monkeypatch.setattr('requests.get', lambda *args, **kw: response)
    with runner.isolated_filesystem():
        result = runner.invoke(cli, ['login'], catch_exceptions=False, input='pieroneurl\n')
        assert 'ERROR: Could not reach https://pieroneurl' in result.output
        assert result.exit_code == 1
        assert not os.path.exists(os.path.join(str(tmpdir), '.docker/config.json'))
class CommandsProposalVersionsTests(TestCase):
    def setUp(self):
        self.cli = CliRunner()

    def test_missing_notice(self):
        """We should get an exception if the notice isn't present"""
        with self.cli.isolated_filesystem():
            result = self.cli.invoke(proposal_versions, ['1111'])
            self.assertTrue(isinstance(result.exception, dependency.Missing))
            self.assertEqual(result.exception.dependency,
                             str(entry.Notice('1111')))

    @patch('regparser.commands.proposal_versions.entry')
    def test_creates_version(self, entry):
        notice = entry.Notice.return_value.read.return_value
        notice.published = date.today()
        notice.cfr_ref_pairs = [(11, 111), (11, 222), (22, 222), (22, 333)]
        with self.cli.isolated_filesystem():
            result = self.cli.invoke(proposal_versions, ['dddd'])
            self.assertIsNone(result.exception)
            self.assertEqual('dddd', entry.Notice.call_args[0][0])
            self.assertEqual([lst[0] for lst in entry.Version.call_args_list],
                             [(11, 111, 'dddd'), (11, 222, 'dddd'),
                              (22, 222, 'dddd'), (22, 333, 'dddd')])
            self.assertEqual(entry.Version.return_value.write.call_args[0][0],
                             Version('dddd', date.today(), None))
Ejemplo n.º 4
0
    def test_drop_fail(self, connect_db_mock):
        ''' The "drop" CLI command should fail gracefully. '''

        runner = CliRunner()
        cursor = self.make_cursor(connect_db_mock)
        cursor.execute.side_effect = ValueError
        drop_table_re = re.compile(r'drop\s+table', flags=re.IGNORECASE)

        # Non-debug mode returns non-zero exit code.
        with runner.isolated_filesystem():
            self.make_sample_files()
            cli_args1 = self.cli_args() + ['drop']
            result1 = runner.invoke(agnostic.cli, cli_args1, input='y')

        self.assertNotEqual(0, result1.exit_code)
        self.assertIsInstance(result1.exception, SystemExit)

        # Debug mode raises exception.
        with runner.isolated_filesystem():
            self.make_sample_files()
            cli_args2 = self.cli_args() + ['--debug', 'drop']
            result2 = runner.invoke(agnostic.cli, cli_args2, input='y')

        self.assertNotEqual(0, result2.exit_code)
        self.assertIsInstance(result2.exception, ValueError)
Ejemplo n.º 5
0
    def test_list_invalid_status(self, connect_db_mock):
        '''
        The "list" CLI command fails gracefully if a migration has an invalid
        status.
        '''

        runner = CliRunner()
        cursor = self.make_cursor(connect_db_mock)
        cursor.fetchall.return_value = [
            ('01/bar', 'bootstrapped', datetime.now(), datetime.now()),
            ('01/foo', 'bogus-status', datetime.now(), datetime.now()),
        ]

        # In non-debug mode, it exists with a non-zero status.
        with runner.isolated_filesystem():
            self.make_sample_files()
            cli_args = self.cli_args() + ['list']
            result1 = runner.invoke(agnostic.cli, cli_args)

        self.assertNotEqual(0, result1.exit_code)
        self.assertIsInstance(result1.exception, SystemExit)

        # In debug mode, it raises an exception.
        with runner.isolated_filesystem():
            self.make_sample_files()
            cli_args = self.cli_args() + ['--debug', 'list']
            result2 = runner.invoke(agnostic.cli, cli_args)

        self.assertNotEqual(0, result2.exit_code)
        self.assertIsInstance(result2.exception, ValueError)
Ejemplo n.º 6
0
def test_metrics():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    runner = CliRunner()
    result = runner.invoke(commands.metrics, [dev1])
    with runner.isolated_filesystem():
        with open('tmp', 'w') as f:
            f.write(result.output)
        test_ref = bob.io.base.test_utils.datafile('test_m1.txt', 'bob.measure')
        assert_click_runner_result(result)

    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    with runner.isolated_filesystem():
        result = runner.invoke(
            commands.metrics, ['-e', dev1, test1, dev2, test2]
        )
        with open('tmp', 'w') as f:
            f.write(result.output)
        test_ref = bob.io.base.test_utils.datafile('test_m2.txt', 'bob.measure')
        assert_click_runner_result(result)
    with runner.isolated_filesystem():
        result = runner.invoke(
            commands.metrics, ['-e', '-l', 'tmp', dev1, test1, dev2, test2, '-lg',
                              'A,B']
        )
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(
            commands.metrics, ['-l', 'tmp', dev1, dev2]
        )
        assert_click_runner_result(result)
Ejemplo n.º 7
0
class DeployTest(unittest.TestCase):
    def setUp(self):
        self.runner = CliRunner()
        os.environ['SHUB_APIKEY'] = '1234'

    def test_fails_when_deploy_is_invoked_outside_of_a_scrapy_project(self):
        # given there's no scrapy.cfg file in the current folder
        with self.runner.isolated_filesystem():
            # when
            result = self.runner.invoke(deploy.cli)

            # then
            self.assertEqual(1, result.exit_code)

    @patch('shub.deploy.make_deploy_request')
    def test_parses_project_cfg_and_uploads_egg(self, deploy_req_mock):
        # given
        valid_scrapy_cfg = """
[deploy]
username = API_KEY
project = -1

[settings]
default = project.settings
"""
        with self.runner.isolated_filesystem():
            with open('scrapy.cfg', 'w') as f:
                f.write(valid_scrapy_cfg)

            # when
            result = self.runner.invoke(deploy.cli)

            # then
            err = 'Output: %s\nException: %s' % (result.output, result.exception)
            self.assertEqual(0, result.exit_code, err)
Ejemplo n.º 8
0
def test_cmc():
    dev1 = pkg_resources.resource_filename('bob.bio.base.test',
                                           'data/scores-cmc-5col.txt')
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(commands.cmc, [dev1])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
    test1 = pkg_resources.resource_filename('bob.bio.base.test',
                                            'data/scores-cmc-4col.txt')
    with runner.isolated_filesystem():
        result = runner.invoke(commands.cmc, ['--output', 'test.pdf',
                                              '--legends', 'A,B', '-S',
                                              '-ts', 'TA,TB', '-e',
                                              dev1, test1, dev1, test1])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    dev_nonorm = pkg_resources.resource_filename('bob.bio.base.test',
                                                 'data/scores-nonorm-dev')
    dev_ztnorm = pkg_resources.resource_filename('bob.bio.base.test',
                                                 'data/scores-ztnorm-dev')

    with runner.isolated_filesystem():
        result = runner.invoke(commands.cmc, [
            '-v', '--legends', 'A', '-e',
            dev_nonorm, dev_ztnorm
        ])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
Ejemplo n.º 9
0
def test_hist():
    dev1 = pkg_resources.resource_filename('bob.bio.base.test',
                                           'data/dev-4col.txt')
    dev2 = pkg_resources.resource_filename('bob.bio.base.test',
                                           'data/dev-5col.txt')
    test1 = pkg_resources.resource_filename('bob.bio.base.test',
                                            'data/test-4col.txt')
    test2 = pkg_resources.resource_filename('bob.bio.base.test',
                                            'data/test-5col.txt')
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, [dev1])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, ['--criterion', 'min-hter', '--output',
                                               'HISTO.pdf', '-b',
                                               '30,auto', dev1, dev2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, ['--criterion', 'eer', '--output',
                                               'HISTO.pdf', '-b', '30', '-e',
                                               '-ts', 'A,B', dev1, test1, dev2,
                                               test2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
Ejemplo n.º 10
0
def test_hist():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, [dev1])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, ['--criterion',
                                               'min-hter', '--no-line',
                                               '--output', 'HISTO.pdf',  '-b',
                                               '30,100', dev1, dev2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(commands.hist, ['-e', '--criterion', 'eer','--output',
                                               'HISTO.pdf',  '-b',  '30,20',
                                               '-sp', 111, '-ts', 'A,B',
                                               dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
Ejemplo n.º 11
0
def test_det():
    dev1 = bob.io.base.test_utils.datafile('dev-1.txt', 'bob.measure')
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(commands.det, [dev1])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
    dev2 = bob.io.base.test_utils.datafile('dev-2.txt', 'bob.measure')
    test1 = bob.io.base.test_utils.datafile('test-1.txt', 'bob.measure')
    test2 = bob.io.base.test_utils.datafile('test-2.txt', 'bob.measure')
    with runner.isolated_filesystem():
        result = runner.invoke(commands.det, ['-e', '--split', '--output',
                                              'test.pdf', '--legends', 'A,B',
                                              '-ll', 'upper-right',
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(commands.det, ['--output',
                                              'test.pdf', '-e',
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
class CommandsFetchSxSTests(TestCase):
    def setUp(self):
        super(CommandsFetchSxSTests, self).setUp()
        self.cli = CliRunner()
        with XMLBuilder("ROOT") as ctx:
            ctx.PRTPAGE(P="1234")
            ctx.CFR('12 CFR 1000')
        self.notice_xml = NoticeXML(ctx.xml)

    def test_missing_notice(self):
        """If the necessary notice XML is not present, we should expect a
        dependency error"""
        with self.cli.isolated_filesystem():
            result = self.cli.invoke(fetch_sxs, ['1111'])
            self.assertTrue(isinstance(result.exception, dependency.Missing))

    @patch('regparser.commands.fetch_sxs.build_notice')
    @patch('regparser.commands.fetch_sxs.meta_data')
    def test_writes(self, meta_data, build_notice):
        """If the notice XML is present, we write the parsed version to disk,
        even if that version's already present"""
        with self.cli.isolated_filesystem():
            entry.Notice('1111').write(self.notice_xml)
            self.cli.invoke(fetch_sxs, ['1111'])
            meta_data.return_value = {'example': 1}
            self.assertTrue(build_notice.called)
            args, kwargs = build_notice.call_args
            self.assertTrue(args[2], {'example': 1})
            self.assertTrue(
                isinstance(kwargs['xml_to_process'], etree._Element))

            build_notice.reset_mock()
            entry.Entry('rule_changes', '1111').write('content')
            self.cli.invoke(fetch_sxs, ['1111'])
            self.assertTrue(build_notice.called)
Ejemplo n.º 13
0
class TestDeployReqs(unittest.TestCase):
    VALID_APIKEY = '1234'

    def setUp(self):
        self.runner = CliRunner()

    def cli_run(self, cli, args):
        return self.runner.invoke(cli, args, env={'SHUB_APIKEY': self.VALID_APIKEY})

    def test_can_decompress_downloaded_packages_and_call_deploy_reqs(self, deploy_egg_mock):
        # GIVEN
        requirements_file = self._write_tmp_requirements_file()

        with self.runner.isolated_filesystem():
            # WHEN
            result = self.cli_run(deploy_reqs.cli, ["-p -1", requirements_file])

            # THEN
            self.assertEqual(2, deploy_egg_mock.call_count, self.error_for(result))

    def test_uses_project_id_from_scrapy_cfg_per_default(self, deploy_egg_mock):
        requirements_file = self._write_tmp_requirements_file()
        with self.runner.isolated_filesystem():
            # GIVEN
            self.write_valid_scrapy_cfg()

            # WHEN I don't provide the project id
            self.cli_run(deploy_reqs.cli, [requirements_file])

            # THEN It uses the project id in the scrapy.cfg file
            deploy_egg_mock.assert_called_with('-1', self.VALID_APIKEY)

    def _write_tmp_requirements_file(self):
        basepath = 'tests/samples/deploy_reqs_sample_project/'
        eggs = ['other-egg-0.2.1.zip', 'inflect-0.2.5.tar.gz']
        tmp_dir = tempfile.mkdtemp(prefix="shub-test-deploy-reqs")
        requirements_file = os.path.join(tmp_dir, 'requirements.txt')

        with open(requirements_file, 'w') as f:
            for egg in eggs:
                f.write(os.path.abspath(os.path.join(basepath, egg)) + "\n")

        return requirements_file

    def write_valid_scrapy_cfg(self):

        valid_scrapy_cfg = """
[deploy]
username = API_KEY
project = -1

[settings]
default = project.settings
"""
        with open('scrapy.cfg', 'w') as f:
            f.write(valid_scrapy_cfg)

    def error_for(self, result):
        return '\nOutput: %s.\nException: %s' % (result.output.strip(), repr(result.exception))
class LoginTest(unittest.TestCase):
    VALID_KEY = 32 * "1"

    def setUp(self):
        login.auth.NETRC_FILE = ""
        self.runner = CliRunner()

    def test_login_validates_api_key(self):
        result = self.runner.invoke(login.cli, input="invalid key")
        self.assertEqual(2, result.exit_code)
        self.assertTrue("Invalid key" in result.output)

    def test_login_writes_input_key_to_netrc_file(self):
        # given
        fake_netrc_writer = Mock()
        login.auth.write_key_netrc = fake_netrc_writer

        # when
        self.runner.invoke(login.cli, input=self.VALID_KEY)

        # then
        fake_netrc_writer.assert_called_with(self.VALID_KEY)

    def test_login_suggests_scrapy_cfg_username_as_key(self):
        scrapy_cfg_with_username = """
[deploy]
username = KEY_SUGGESTION
        """

        with self.runner.isolated_filesystem():
            with open("scrapy.cfg", "w") as f:
                f.write(scrapy_cfg_with_username)

            result = self.runner.invoke(login.cli, input="123")
            err = "Unexpected output: %s" % result.output
            self.assertTrue("KEY_SUGGESTION" in result.output, err)

    def test_login_suggests_shub_apikey_as_key(self):
        result = self.runner.invoke(login.cli, input="123", env={"SHUB_APIKEY": "SHUB_APIKEY_VALUE"})

        err = "Unexpected output: %s" % result.output
        self.assertTrue("SHUB_APIKEY_VALUE" in result.output, err)

    def test_login_can_handle_invalid_scrapy_cfg(self):
        # given
        invalid_scrapy_cfg = "invalid content"

        with self.runner.isolated_filesystem() as fs:
            login.auth.NETRC_FILE = os.path.join(fs, ".netrc")

            with open("scrapy.cfg", "w") as f:
                f.write(invalid_scrapy_cfg)

            # when
            result = self.runner.invoke(login.cli, input=self.VALID_KEY)

            # then
            self.assertEqual(0, result.exit_code, result.exception)
Ejemplo n.º 15
0
def test_create_application(monkeypatch):
    monkeypatch.setattr('boto.ec2.connect_to_region', MagicMock())
    monkeypatch.setattr('boto.iam.connect_to_region', MagicMock())
    monkeypatch.setattr('time.sleep', lambda s: s)
    mock_sgr = MagicMock()
    monkeypatch.setattr('aws_minion.cli.SecurityGroupRule', mock_sgr)

    context = Context({'region': 'caprica', 'vpc': 'myvpc'})
    context.get_application = raise_application_not_found
    context_constructor = lambda x, y: context

    monkeypatch.setattr('aws_minion.cli.Context', context_constructor)

    runner = CliRunner()

    data = {
        'application_name': 'myapp',
        'team_name': 'MyTeam',
        'exposed_ports': [123],
        'exposed_protocol': 'http'
    }

    mock_sgr.reset_mock()

    with runner.isolated_filesystem():
        with open('myapp.yaml', 'w') as fd:
            yaml.dump(data, fd)

        context.write_config('config.yaml')

        result = runner.invoke(cli, ['-p', 'default', '--config-file', 'config.yaml', 'applications', 'create', 'myapp.yaml'], catch_exceptions=False)

    assert call('tcp', 80, 80, '0.0.0.0/0', None) in mock_sgr.call_args_list
    assert call('tcp', 443, 443, '0.0.0.0/0', None) in mock_sgr.call_args_list

    assert 'Creating IAM role and instance profile.. OK' in result.output

    data_tcp = {
        'application_name': 'myapp',
        'team_name': 'MyTeam',
        'exposed_ports': [123],
        'exposed_protocol': 'tcp'
    }

    mock_sgr.reset_mock()

    with runner.isolated_filesystem():
        with open('myapp.yaml', 'w') as fd:
            yaml.dump(data_tcp, fd)

        context.write_config('config.yaml')

        result = runner.invoke(cli, ['-p', 'default', '--config-file', 'config.yaml', 'applications', 'create', 'myapp.yaml'], catch_exceptions=False)

    assert mock_sgr.call_args == call('tcp', 123, 123, None, 'app-myapp-lb')
    assert 'Creating IAM role and instance profile.. OK' in result.output
class CommandsFillWithRulesTests(TestCase):
    def setUp(self):
        self.cli = CliRunner()

    def test_dependencies(self):
        """Expect nonexistent trees to depend on their predecessor, associated
        rule changes and version files. Shouldn't add dependencies for the
        first version, if missing"""
        with self.cli.isolated_filesystem():
            version_ids = ['111', '222', '333', '444', '555', '666']
            tree_dir = entry.Tree('12', '1000')
            rule_dir = entry.RuleChanges()
            vers_dir = entry.Version('12', '1000')
            # Existing trees
            (tree_dir / '222').write(Node())
            (tree_dir / '555').write(Node())

            deps = fill_with_rules.dependencies(
                tree_dir, version_ids, '12', '1000')

            # First is skipped, as we can't build it from a rule
            self.assertNotIn(str(tree_dir / '111'), deps)
            # Second can also be skipped as a tree already exists
            self.assertEqual(deps.dependencies(str(tree_dir / '222')), [])
            # Third relies on the associated versions and the second tree
            self.assertItemsEqual(
                deps.dependencies(str(tree_dir / '333')),
                [str(tree_dir / '222'), str(rule_dir / '333'),
                 str(vers_dir / '333')])
            # Fourth relies on the third, even though it's not been built
            self.assertItemsEqual(
                deps.dependencies(str(tree_dir / '444')),
                [str(tree_dir / '333'), str(rule_dir / '444'),
                 str(vers_dir / '444')])
            # Fifth can be skipped as the tree already exists
            self.assertEqual(deps.dependencies(str(tree_dir / '555')), [])
            # Six relies on the fifth
            self.assertItemsEqual(
                deps.dependencies(str(tree_dir / '666')),
                [str(tree_dir / '555'), str(rule_dir / '666'),
                 str(vers_dir / '666')])

    def test_derived_from_rules(self):
        """Should filter a set of version ids to only those with a dependency
        on changes derived from a rule"""
        with self.cli.isolated_filesystem():
            tree_dir = entry.Tree('12', '1000')

            deps = dependency.Graph()
            deps.add(tree_dir / 111, entry.Annual(12, 1000, 2001))
            deps.add(tree_dir / 222, entry.RuleChanges(222))
            deps.add(tree_dir / 333, entry.RuleChanges(333))
            deps.add(tree_dir / 333, entry.Version(333))
            derived = fill_with_rules.derived_from_rules(
                ['111', '222', '333', '444'], deps, tree_dir)
            self.assertEqual(derived, ['222', '333'])
class CommandsLayersTests(TestCase):
    def setUp(self):
        self.cli = CliRunner()

    def test_stale_layers(self):
        """We should have dependencies between all of the layers and their
        associated trees. We should also tie the meta layer to the version"""
        configured_layers = {'cfr': {'keyterms': None, 'other': None}}
        with self.cli.isolated_filesystem(), patch.dict(
                layers.LAYER_CLASSES, configured_layers):
            version_entry = entry.Version(111, 22, 'aaa')
            version_entry.write(Version('aaa', date.today(), date.today()))
            tree_entry = entry.Tree(111, 22, 'aaa')
            # Use list() to instantiate
            self.assertRaises(dependency.Missing,
                              list, layers.stale_layers(tree_entry, 'cfr'))

            entry.Entry('tree', 111, 22, 'bbb').write(b'')    # wrong version
            self.assertRaises(dependency.Missing,
                              list, layers.stale_layers(tree_entry, 'cfr'))

            entry.Entry('tree', 111, 22, 'aaa').write(b'')
            six.assertCountEqual(
                self,
                layers.stale_layers(tree_entry, 'cfr'), ['keyterms', 'other'])

            self.assertIn(
                str(version_entry),
                dependency.Graph().dependencies(
                    str(entry.Layer.cfr(111, 22, 'aaa', 'meta'))))

    def test_process_cfr_layers(self):
        """All layers for a single version should get written."""
        with self.cli.isolated_filesystem():
            version_entry = entry.Version(12, 1000, '1234')
            version_entry.write(Version('1234', date.today(), date.today()))
            entry.Tree('12', '1000', '1234').write(Node())

            layers.process_cfr_layers(
                ['keyterms', 'meta'], 12, version_entry)

            self.assertTrue(
                entry.Layer.cfr(12, 1000, '1234', 'keyterms').exists())
            self.assertTrue(
                entry.Layer.cfr(12, 1000, '1234', 'meta').exists())

    def test_process_preamble_layers(self):
        """All layers for a single preamble should get written."""
        with self.cli.isolated_filesystem():
            preamble_entry = entry.Preamble('111_222')
            preamble_entry.write(Node())

            layers.process_preamble_layers(['graphics'], preamble_entry)

            self.assertTrue(
                entry.Layer.preamble('111_222', 'graphics').exists())
class CommandsClearTests(TestCase):
    def setUp(self):
        self.cli = CliRunner()

    def test_no_errors_when_clear(self):
        """Should raise no errors when no cached files are present"""
        with self.cli.isolated_filesystem():
            self.cli.invoke(clear)

    def test_deletes_fr_cache(self):
        with self.cli.isolated_filesystem():
            open('fr_cache.sqlite', 'w').close()
            self.assertTrue(os.path.exists('fr_cache.sqlite'))

            # flag must be present
            self.cli.invoke(clear)
            self.assertTrue(os.path.exists('fr_cache.sqlite'))

            self.cli.invoke(clear, ['--http-cache'])
            self.assertFalse(os.path.exists('fr_cache.sqlite'))

    def test_deletes_index(self):
        with self.cli.isolated_filesystem():
            entry.Entry('aaa', 'bbb').write('ccc')
            entry.Entry('bbb', 'ccc').write('ddd')
            self.assertEqual(1, len(entry.Entry("aaa")))
            self.assertEqual(1, len(entry.Entry("bbb")))

            self.cli.invoke(clear)
            self.assertEqual(0, len(entry.Entry("aaa")))
            self.assertEqual(0, len(entry.Entry("bbb")))

    def test_deletes_can_be_focused(self):
        """If params are provided to delete certain directories, only those
        directories should get removed"""
        with self.cli.isolated_filesystem():
            to_delete = ['delroot/aaa/bbb', 'delroot/aaa/ccc',
                         'root/delsub/aaa', 'root/delsub/bbb']
            to_keep = ['root/othersub/aaa', 'root/aaa',
                       'top-level-file', 'other-root/aaa']

            for path in to_delete + to_keep:
                entry.Entry(*path.split('/')).write('')

            self.cli.invoke(clear, ['delroot', 'root/delsub'])
            self.assertItemsEqual(['top-level-file', 'root', 'other-root'],
                                  list(entry.Entry()))
            self.assertItemsEqual(['othersub', 'aaa'],
                                  list(entry.Entry('root')))
            self.assertItemsEqual(['aaa'],
                                  list(entry.Entry('other-root')))
Ejemplo n.º 19
0
class DeployTest(unittest.TestCase):

    def setUp(self):
        self.runner = CliRunner()
        self.conf = mock_conf(self, 'shub.deploy.load_shub_config')

    def _make_project(self):
        with open('scrapy.cfg', 'w') as f:
            f.write(VALID_SCRAPY_CFG)

    @patch('shub.deploy.make_deploy_request')
    def test_detect_scrapy_project(self, mock_deploy_req):
        with self.runner.isolated_filesystem():
            result = self.runner.invoke(deploy.cli)
            self.assertEqual(1, result.exit_code)
            self._make_project()
            result = self.runner.invoke(deploy.cli)
            self.assertEqual(0, result.exit_code)

    @patch('shub.deploy.make_deploy_request')
    def _invoke_with_project(self, args, mock_deploy_req):
        with self.runner.isolated_filesystem():
            self._make_project()
            self.runner.invoke(deploy.cli, args)
        return mock_deploy_req.call_args[0]

    def test_fallback_to_default(self):
        url, data, files, auth, _, _ = self._invoke_with_project(None)
        self.assertIn(self.conf.endpoints['default'], url)
        self.assertEqual(data, {'project': 1, 'version': 'version'})
        self.assertEqual(auth, (self.conf.apikeys['default'], ''))

    def test_with_target(self):
        url, data, files, auth, _, _ = self._invoke_with_project(('prod', ))
        self.assertIn(self.conf.endpoints['default'], url)
        self.assertEqual(data, {'project': 2, 'version': 'version'})
        self.assertEqual(auth, (self.conf.apikeys['default'], ''))

    def test_with_id(self):
        url, data, files, auth, _, _ = self._invoke_with_project(('123', ))
        self.assertIn(self.conf.endpoints['default'], url)
        self.assertEqual(data, {'project': 123, 'version': 'version'})
        self.assertEqual(auth, (self.conf.apikeys['default'], ''))

    def test_with_external_id(self):
        url, data, files, auth, _, _ = self._invoke_with_project(
            ('vagrant/456', ))
        self.assertIn(self.conf.endpoints['vagrant'], url)
        self.assertEqual(data, {'project': 456, 'version': 'version'})
        self.assertEqual(auth, (self.conf.apikeys['vagrant'], ''))
Ejemplo n.º 20
0
def test_roc():
    dev1 = pkg_resources.resource_filename('bob.bio.base.test',
                                           'data/dev-4col.txt')
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(commands.roc, ['--output',
                                              'test.pdf',dev1])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
    dev2 = pkg_resources.resource_filename('bob.bio.base.test',
                                           'data/dev-5col.txt')
    test1 = pkg_resources.resource_filename('bob.bio.base.test',
                                            'data/test-4col.txt')
    test2 = pkg_resources.resource_filename('bob.bio.base.test',
                                            'data/test-5col.txt')
    with runner.isolated_filesystem():
        result = runner.invoke(commands.roc, ['--split', '--output',
                                              'test.pdf', '-S', '-ll',
                                              'lower-left', '-e',
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    with runner.isolated_filesystem():
        result = runner.invoke(commands.roc, ['--output',
                                              'test.pdf',
                                              '-e', '--legends', 'A,B',
                                              dev1, test1, dev2, test2])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)

    dev_nonorm = pkg_resources.resource_filename('bob.bio.base.test',
                                                 'data/scores-nonorm-dev')
    dev_ztnorm = pkg_resources.resource_filename('bob.bio.base.test',
                                                 'data/scores-ztnorm-dev')
    with runner.isolated_filesystem():
        result = runner.invoke(commands.roc, [
            '--min-far-value', '1e-6',
            '--lines-at', '1e-5',
            '-v', '--legends', 'A', '-e',
            dev_nonorm, dev_ztnorm
        ])
        if result.output:
            click.echo(result.output)
        assert_click_runner_result(result)
Ejemplo n.º 21
0
def test_error_verbosity_003(caplog):
    """
    Testing debug level verbosity on setting error
    """
    runner = CliRunner()

    # Temporary isolated current dir
    with runner.isolated_filesystem():
        test_cwd = os.getcwd()

        # Silent
        result = runner.invoke(cli_frontend, ['-v 5', 'compile'])

        error_msg = 'Unable to find any settings in directory: {}'.format(
            test_cwd
        )

        assert result.exit_code == 1

        assert caplog.record_tuples == [
            (
                'boussole',
                20,
                'Building project'
            ),
            (
                'boussole',
                50,
                error_msg
            )
        ]
        assert error_msg in result.output
        assert 'Aborted!' in result.output
Ejemplo n.º 22
0
def test_error_verbosity_001(caplog, options, filename):
    """
    Testing default verbosity (aka INFO level) on setting error with
    different backends
    """
    runner = CliRunner()

    # Temporary isolated current dir
    with runner.isolated_filesystem():
        test_cwd = os.getcwd()

        # Default verbosity
        result = runner.invoke(cli_frontend, ['compile']+options)

        assert result.exit_code == 1

        assert caplog.record_tuples == [
            (
                'boussole',
                20,
                'Building project'
            ),
            (
                'boussole',
                50,
                'Unable to find any settings in directory: {}'.format(test_cwd)
            )
        ]


        assert 'Aborted!' in result.output
Ejemplo n.º 23
0
def test_error_verbosity_002(caplog):
    """
    Testing silent on setting error
    """
    runner = CliRunner()

    # Temporary isolated current dir
    with runner.isolated_filesystem():
        test_cwd = os.getcwd()

        # Silent
        result = runner.invoke(cli_frontend, ['-v 0', 'compile'])

        error_msg = 'Unable to find any settings in directory: {}'.format(
            test_cwd
        )

        assert result.exit_code == 1

        assert caplog.record_tuples == [
            (
                'boussole',
                50,
                error_msg
            )
        ]

        # Totally silent output excepted the one from click.Abort()
        assert error_msg not in result.output
        assert 'Aborted!' in result.output
Ejemplo n.º 24
0
def test_images(monkeypatch):
    image = MagicMock()
    image.id = 'ami-123'
    image.name = 'BrandNewImage'
    image.creationDate = datetime.datetime.utcnow().isoformat('T') + 'Z'

    old_image_still_used = MagicMock()
    old_image_still_used.id = 'ami-456'
    old_image_still_used.name = 'OldImage'
    old_image_still_used.creationDate = (datetime.datetime.utcnow() - datetime.timedelta(days=30)).isoformat('T') + 'Z'

    instance = MagicMock()
    instance.id = 'i-777'
    instance.image_id = 'ami-456'
    instance.tags = {'aws:cloudformation:stack-name': 'mystack'}

    ec2 = MagicMock()
    ec2.get_all_images.return_value = [image, old_image_still_used]
    ec2.get_only_instances.return_value = [instance]
    monkeypatch.setattr('boto.cloudformation.connect_to_region', lambda x: MagicMock())
    monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: ec2)
    monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())

    runner = CliRunner()

    with runner.isolated_filesystem():
        result = runner.invoke(cli, ['images', '--region=myregion'], catch_exceptions=False)

    assert 'ami-123' in result.output
    assert 'ami-456' in result.output
    assert 'mystack' in result.output
Ejemplo n.º 25
0
def test_print_basic(monkeypatch):
    monkeypatch.setattr('boto.cloudformation.connect_to_region', lambda x: MagicMock())
    monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())

    data = {'SenzaInfo': {'StackName': 'test'}, 'SenzaComponents': [{'Configuration': {'Type': 'Senza::Configuration',
                                                                                       'ServerSubnets': {
                                                                                           'eu-west-1': [
                                                                                               'subnet-123']}}},
                                                                    {'AppServer': {
                                                                        'Type': 'Senza::TaupageAutoScalingGroup',
                                                                        'InstanceType': 't2.micro',
                                                                        'Image': 'AppImage',
                                                                        'TaupageConfig': {'runtime': 'Docker',
                                                                                          'source': 'foo/bar'}}}]}

    runner = CliRunner()

    with runner.isolated_filesystem():
        with open('myapp.yaml', 'w') as fd:
            yaml.dump(data, fd)

        result = runner.invoke(cli, ['print', 'myapp.yaml', '--region=myregion', '123', '1.0-SNAPSHOT'],
                               catch_exceptions=False)

    assert 'AWSTemplateFormatVersion' in result.output
    assert 'subnet-123' in result.output
    def test_prompt_fig_quiet(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open('fig.yml', 'w') as f:
                f.write(self.yaml_input)

            result = runner.invoke(transform, ['fig.yml', '-q'])
            assert result.exit_code == 0

            data = json.loads(result.output)

            self.assertIn(
                {
                    'name': 'web',
                    'image': 'me/myapp',
                    'memory': 4,
                    'essential': True
                },
                data['containerDefinitions'],
            )
            self.assertIn(
                {
                    'name': 'web2',
                    'memory': 4,
                    'essential': True
                },
                data['containerDefinitions'],
            )
Ejemplo n.º 27
0
def test_console(monkeypatch):
    stack = MagicMock(stack_name='test-1')
    inst = MagicMock()
    inst.tags = {'aws:cloudformation:stack-name': 'test-1'}
    ec2 = MagicMock()
    ec2.get_only_instances.return_value = [inst]
    ec2.get_console_output.return_value.output = b'**MAGIC-CONSOLE-OUTPUT**'
    monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: ec2)
    monkeypatch.setattr('boto.cloudformation.connect_to_region',
                        lambda x: MagicMock(list_stacks=lambda stack_status_filters: [stack]))
    monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())

    runner = CliRunner()

    data = {'SenzaInfo': {'StackName': 'test'}}

    with runner.isolated_filesystem():
        with open('myapp.yaml', 'w') as fd:
            yaml.dump(data, fd)
        result = runner.invoke(cli, ['console', 'myapp.yaml', '--region=myregion', '1'],
                               catch_exceptions=False)

        assert '**MAGIC-CONSOLE-OUTPUT**' in result.output

        result = runner.invoke(cli, ['console', 'foobar', '--region=myregion'],
                               catch_exceptions=False)
        assert '' == result.output

        result = runner.invoke(cli, ['console', '172.31.1.2', '--region=myregion'],
                               catch_exceptions=False)
        assert '**MAGIC-CONSOLE-OUTPUT**' in result.output

        result = runner.invoke(cli, ['console', 'i-123', '--region=myregion'],
                               catch_exceptions=False)
        assert '**MAGIC-CONSOLE-OUTPUT**' in result.output
Ejemplo n.º 28
0
def test_delete(monkeypatch):
    cf = MagicMock()
    stack = MagicMock(stack_name='test-1')
    cf.list_stacks.return_value = [stack]
    monkeypatch.setattr('boto.cloudformation.connect_to_region', lambda x: cf)
    monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())

    runner = CliRunner()

    data = {'SenzaInfo': {'StackName': 'test'}}

    with runner.isolated_filesystem():
        with open('myapp.yaml', 'w') as fd:
            yaml.dump(data, fd)
        result = runner.invoke(cli, ['delete', 'myapp.yaml', '--region=myregion', '1'],
                               catch_exceptions=False)
        assert 'OK' in result.output

        cf.list_stacks.return_value = [stack, stack]
        result = runner.invoke(cli, ['delete', 'myapp.yaml', '--region=myregion'],
                               catch_exceptions=False)
        assert 'Please use the "--force" flag if you really want to delete multiple stacks' in result.output

        result = runner.invoke(cli, ['delete', 'myapp.yaml', '--region=myregion', '--force'],
                               catch_exceptions=False)
        assert 'OK' in result.output
Ejemplo n.º 29
0
def test_arg_part_missing():
    """bamp"""
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(bamp)
        assert result.exit_code == 2
        assert 'Missing argument "[patch|minor|major]"' in result.output
Ejemplo n.º 30
0
def test_print_replace_mustache(monkeypatch):
    sg = MagicMock()
    sg.name = 'app-master-mind'
    sg.id = 'sg-007'

    monkeypatch.setattr('boto.cloudformation.connect_to_region', lambda x: MagicMock())
    monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: MagicMock(get_all_security_groups=lambda: [sg]))
    monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())
    data = {'SenzaInfo': {'StackName': 'test',
                          'Parameters': [{'ApplicationId': {'Description': 'Application ID from kio'}}]},
            'SenzaComponents': [{'Configuration': {'ServerSubnets': {'eu-west-1': ['subnet-123']},
                                                   'Type': 'Senza::Configuration'}},
                                {'AppServer': {'Image': 'AppImage',
                                               'InstanceType': 't2.micro',
                                               'SecurityGroups': ['app-{{Arguments.ApplicationId}}'],
                                               'IamRoles': ['app-{{Arguments.ApplicationId}}'],
                                               'TaupageConfig': {'runtime': 'Docker',
                                                                 'source': 'foo/bar'},
                                               'Type': 'Senza::TaupageAutoScalingGroup'}}]
            }

    runner = CliRunner()

    with runner.isolated_filesystem():
        with open('myapp.yaml', 'w') as fd:
            yaml.dump(data, fd)

        result = runner.invoke(cli, ['print', 'myapp.yaml', '--region=myregion', '123', 'master-mind'],
                               catch_exceptions=False)
    assert 'AWSTemplateFormatVersion' in result.output
    assert 'subnet-123' in result.output
    assert 'app-master-mind' in result.output
    assert 'sg-007' in result.output
Ejemplo n.º 31
0
def test_airtable_sqlite(mocked):
    runner = CliRunner()
    with runner.isolated_filesystem():
        args = [
            ".",
            "appZOGvNJPXCQ205F",
            "tablename",
            "--key",
            "x",
            "--sqlite",
            "test.db",
        ]
        result = runner.invoke(cli.cli, args, catch_exceptions=False)
        assert 0 == result.exit_code, result.stdout
        db = sqlite_utils.Database("test.db")
        assert db.table_names() == ["tablename"]
        assert list(db["tablename"].rows) == [
            {
                "airtable_id": "rec1",
                "name": "This is the name",
                "address": "Address line 1\nAddress line 2",
                "weird name: what is this?": "hello",
                "size": 441,
                "true_or_false": 1,
                "airtable_createdTime": "2020-04-18T18:50:27.000Z",
            },
            {
                "airtable_id": "rec2",
                "name": "This is the name 2",
                "address": "Address line 1\nAddress line 2",
                "weird name: what is this?": "there",
                "size": 442,
                "true_or_false": 0,
                "airtable_createdTime": "2020-04-18T18:58:27.000Z",
            },
        ]
Ejemplo n.º 32
0
def test_password_nonempty_file():
    """Test the encrypt module's CLI function with a nonempty YAML file.

    The YAML file includes information that needs to be overwritten."""
    runner = CliRunner()
    with runner.isolated_filesystem():

        initial_data = OrderedDict([('language', 'python'), ('dist', 'trusty'),
                                    ('password', {'secure': 'SUPER_INSECURE_PASSWORD'})])

        with open('file.yml', 'w') as file:
            ordered_dump(initial_data, file)

        result = runner.invoke(cli, ['mandeep', 'Travis-Encrypt', 'file.yml'],
                               'SUPER_SECURE_PASSWORD')
        assert not result.exception

        with open('file.yml') as file:
            config = ordered_load(file)

        assert config['language'] == 'python'
        assert config['dist'] == 'trusty'
        assert base64.b64decode(config['password']['secure'])
        assert ['language', 'dist', 'password'] == [key for key in config.keys()]
Ejemplo n.º 33
0
    def test_make_submission_when_file_is_valid_with_metadata(self):
        expected = "Do you want to include the Submission Details? [y/N]: Y"
        expected = "{}\n{}".format(
            expected,
            ("Method Name []: Test\nMethod Description []: "
             "Test\nProject URL []: Test\nPublication URL []: Test\n"),
        )
        expected = "{}{}".format(
            expected,
            "Do you want to include the Submission Metadata? [y/N]: Y\n",
        )
        expected = "{}{}".format(
            expected,
            ("TextAttribute* (Sample) []: Test\nSingleOptionAttribute* (Sample):\n"
             "Choices:['A', 'B', 'C'] []: A\nMultipleChoiceAttribute* (Sample):\n"
             "Choices(separated by comma):['alpha', 'beta', 'gamma']: alpha\nTrueFalseField* (Sample) []: True\n"
             ),
        )
        expected = "{}\n{}".format(
            expected,
            ("Your file {} with the ID {} is successfully submitted.\n\n"
             "You can use `evalai submission {}` to view this "
             "submission's status.").format("test_file.txt", "9", "9"),
        )
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open("test_file.txt", "w") as f:
                f.write("1 2 3 4 5 6")

            result = runner.invoke(
                challenge,
                ["1", "phase", "2", "submit", "--file", "test_file.txt"],
                input="Y\nTest\nTest\nTest\nTest\nY\nTest\nA\nalpha\nTrue\n",
            )
            assert result.exit_code == 0
            assert result.output.strip() == expected
Ejemplo n.º 34
0
def test_fts_one_column_multiple_aliases():
    runner = CliRunner()
    with runner.isolated_filesystem():
        open('test.csv', 'w').write(CSV_MULTI)
        result = runner.invoke(
            cli.cli, ('test.csv fts-extracted.db -c film '
                      '-c actor_1:actors:name -c actor_2:actors:name '
                      '-f film -f actor_1 -f actor_2').split())
        assert result.exit_code == 0
        conn = sqlite3.connect('fts-extracted.db')
        assert [
            ('The Rock', 'Sean Connery', 'Nicolas Cage'),
            ('National Treasure', 'Nicolas Cage', 'Diane Kruger'),
            ('Troy', 'Diane Kruger', 'Orlando Bloom'),
        ] == conn.execute('''
            select
                film.value, a1.name, a2.name
            from test
                join film on test.film = film.id
                join actors a1 on test.actor_1 = a1.id
                join actors a2 on test.actor_2 = a2.id
        ''').fetchall()
        assert [
            ('National Treasure', 'Nicolas Cage', 'Diane Kruger'),
            ('Troy', 'Diane Kruger', 'Orlando Bloom'),
        ] == conn.execute('''
            select
                film.value, a1.name, a2.name
            from test
                join film on test.film = film.id
                join actors a1 on test.actor_1 = a1.id
                join actors a2 on test.actor_2 = a2.id
            where test.rowid in (
                select rowid from [test_fts] where [test_fts] match 'kruger'
            )
        ''').fetchall()
Ejemplo n.º 35
0
def test_convert_scalar_float_labels(tmp_path):
    runner = CliRunner()
    with runner.isolated_filesystem():
        csvpath = get_data(str(tmp_path))
        # Make labels scalars.
        data = [(x, 1.0) for (x, _) in read_csv(csvpath)]
        csvpath = tmp_path.with_suffix(".new.csv")
        with open(csvpath, "w", newline="") as myfile:
            wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
            wr.writerows(data)
        tfrecords_template = Path("data/shard-{shard:03d}.tfrecords")
        tfrecords_template.parent.mkdir(exist_ok=True)
        args = """\
    convert --csv={} --tfrecords-template={} --volume-shape 256 256 256
        --examples-per-shard=2 --to-ras --no-verify-volumes
    """.format(csvpath, tfrecords_template)
        result = runner.invoke(climain.cli, args.split())
        assert result.exit_code == 0
        assert Path("data/shard-000.tfrecords").is_file()
        assert Path("data/shard-001.tfrecords").is_file()
        assert Path("data/shard-002.tfrecords").is_file()
        assert Path("data/shard-003.tfrecords").is_file()
        assert Path("data/shard-004.tfrecords").is_file()
        assert not Path("data/shard-005.tfrecords").is_file()
Ejemplo n.º 36
0
def test_export():
    import glob
    import os
    import os.path
    import osxphotos
    from osxphotos.__main__ import export

    runner = CliRunner()
    cwd = os.getcwd()
    # pylint: disable=not-context-manager
    with runner.isolated_filesystem():
        result = runner.invoke(
            export,
            [
                os.path.join(cwd, CLI_PHOTOS_DB),
                ".",
                "--original-name",
                "--export-edited",
                "-V",
            ],
        )
        assert result.exit_code == 0
        files = glob.glob("*")
        assert sorted(files) == sorted(CLI_EXPORT_FILENAMES)
Ejemplo n.º 37
0
def test_delete_non_existing_file():
    """Test delete non existing file."""
    status_code = 200
    reana_token = '000000'
    filename = 'file11'
    response = {'deleted': {}, 'failed': {}}
    message = '{} did not match any existing file.'.format(filename)
    mock_http_response = Mock()
    mock_http_response.status_code = status_code
    mock_http_response.raw_bytes = str(response).encode()
    mock_response = response
    env = {'REANA_SERVER_URL': 'localhost'}
    runner = CliRunner(env=env)
    with runner.isolation():
        with patch(
                "reana_client.api.client.current_rs_api_client",
                make_mock_api_client('reana-server')(mock_response,
                                                     mock_http_response)):
            with runner.isolated_filesystem():
                result = runner.invoke(cli, [
                    'rm', '-t', reana_token, '--workflow', 'mytest.1', filename
                ])
                assert result.exit_code == 0
                assert message in result.output
Ejemplo n.º 38
0
def test_publish_cloudrun_prompts_for_service(mock_get_existing_services,
                                              mock_call, mock_output,
                                              mock_which):
    mock_get_existing_services.return_value = [{
        "name": "existing",
        "created": "2019-01-01",
        "url": "http://www.example.com/"
    }]
    mock_output.return_value = "myproject"
    mock_which.return_value = True
    runner = CliRunner()
    with runner.isolated_filesystem():
        open("test.db", "w").write("data")
        result = runner.invoke(cli.cli, ["publish", "cloudrun", "test.db"],
                               input="input-service")
        assert ("""
Please provide a service name for this deployment

Using an existing service name will over-write it

Your existing services:

  existing - created 2019-01-01 - http://www.example.com/

Service name: input-service
""".strip() == result.output.strip())
        assert 0 == result.exit_code
        tag = "gcr.io/myproject/datasette"
        mock_call.assert_has_calls([
            mock.call("gcloud builds submit --tag {}".format(tag), shell=True),
            mock.call(
                "gcloud beta run deploy --allow-unauthenticated --platform=managed --image {} input-service"
                .format(tag),
                shell=True,
            ),
        ])
Ejemplo n.º 39
0
def test_main_fails_trailing_slash_2(runner: CliRunner) -> None:
    """Should return exit_code 0."""
    with runner.isolated_filesystem():
        with open("banker.csv", "w") as f:
            f.write(
                "OrgNummer,Navn,Filnavn,EndepunktProduksjon,EndepunktTest\n")
            f.write("837884942,SPAREBANK 1 ØSTFOLD AKERSHUS,"
                    "Sparebank1_837884942_Accounts-API.json,"
                    "https://api.sparebank1.no/dsop/Service/v2/837884942,"
                    "https://api-test.sparebank1.no/dsop/Service/v2/837884942/"
                    "\n")
        with open("template.yaml", "w") as t:
            t.write("openapi: 3.0.0\n")
            t.write("info:\n")
            t.write("  title: Accounts API\n")
            t.write("servers:\n")
            t.write("  - url: 'https://hostname.no/v1'\n")
            t.write("    description: 'production'\n")
            t.write("  - url: 'https://hostname.no/v1'\n")
            t.write("    description: 'test'\n")

        result = runner.invoke(main, ["template.yaml", "banker.csv"])
        assert result.exit_code == 1
        assert result.output is not None
Ejemplo n.º 40
0
def test_cog_translate_valiNodataNan():
    """Should work as expected and create mask from NaN."""
    runner = CliRunner()
    with runner.isolated_filesystem():
        cog_translate(raster_path_nan,
                      "cogeo_nan.tif",
                      raw_profile,
                      quiet=True)
        with rasterio.open("cogeo_nan.tif") as src:
            assert src.meta["dtype"] == "float64"
            assert src.nodata
            assert not src.dataset_mask().all()

        cog_translate(
            raster_path_float,
            "cogeo_nan.tif",
            raw_profile,
            nodata=numpy.nan,
            quiet=True,
        )
        with rasterio.open("cogeo_nan.tif") as src:
            assert src.meta["dtype"] == "float64"
            assert src.nodata
            assert not src.dataset_mask().all()
Ejemplo n.º 41
0
def test_export_live():
    import glob
    import os
    import os.path
    import osxphotos
    from osxphotos.__main__ import export

    runner = CliRunner()
    cwd = os.getcwd()
    # pylint: disable=not-context-manager
    with runner.isolated_filesystem():
        result = runner.invoke(
            export,
            [
                os.path.join(cwd, LIVE_PHOTOS_DB),
                ".",
                "--live",
                "--original-name",
                "--export-live",
                "-V",
            ],
        )
        files = glob.glob("*")
        assert sorted(files) == sorted(CLI_EXPORT_LIVE_ORIGINAL)
Ejemplo n.º 42
0
def test_publish_heroku_plugin_secrets(mock_call, mock_check_output,
                                       mock_which):
    mock_which.return_value = True
    mock_check_output.side_effect = lambda s: {
        "['heroku', 'plugins']": b"heroku-builds",
        "['heroku', 'apps:list', '--json']": b"[]",
        "['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}',
    }[repr(s)]
    runner = CliRunner()
    with runner.isolated_filesystem():
        open("test.db", "w").write("data")
        result = runner.invoke(
            cli.cli,
            [
                "publish",
                "heroku",
                "test.db",
                "--plugin-secret",
                "datasette-auth-github",
                "client_id",
                "x-client-id",
            ],
        )
        assert 0 == result.exit_code, result.output
        mock_call.assert_has_calls([
            mock.call([
                "heroku",
                "config:set",
                "-a",
                "f",
                "DATASETTE_AUTH_GITHUB_CLIENT_ID=x-client-id",
            ]),
            mock.call(
                ["heroku", "builds:create", "-a", "f",
                 "--include-vcs-ignore"]),
        ])
Ejemplo n.º 43
0
def test_fts_and_extract_columns():
    runner = CliRunner()
    with runner.isolated_filesystem():
        open('test.csv', 'w').write(CSV)
        result = runner.invoke(
            cli.cli,
            ('test.csv fts-extracted.db -c office -c party -c candidate '
             '-f party -f candidate').split())
        assert result.exit_code == 0
        conn = sqlite3.connect('fts-extracted.db')
        assert [
            ('Yolo', 100001, 'President', 'PAF', 'Gloria Estela La Riva'),
        ] == conn.execute('''
            select
                county, precinct, office.value, party.value, candidate.value
            from test
                left join office on test.office = office.id
                left join party on test.party = party.id
                left join candidate on test.candidate = candidate.id
            where test.rowid in (
                select rowid from test_fts
                where test_fts match 'paf gloria'
            )
        ''').fetchall()
Ejemplo n.º 44
0
    def test_make_submission_using_presigned_url(self, request):
        expected = (
            "Do you want to include the Submission Details? [y/N]: N\n"
            "Do you want to include the Submission Metadata? [y/N]: N\n"
            "Uploading the file...\n\n"
            "Your submission test_file.txt with the id 9 is successfully submitted for evaluation.\n\n"
        )
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open("test_file.txt", "w") as f:
                f.write("1 2 3 4 5 6")

            result = runner.invoke(challenge, [
                "1", "phase", "2", "submit", "--file", "test_file.txt",
                "--large"
            ],
                                   input="N\nN")
            response = result.output

            # Remove progress bar from response
            splitted_response = response.split("\n")
            splitted_response.pop(3)
            response = "\n".join(splitted_response)
            assert response == expected
Ejemplo n.º 45
0
def test_cog_validate_validCreatioValid(monkeypatch):
    """Should work as expected (validate cogeo file)."""
    runner = CliRunner()
    with runner.isolated_filesystem():
        cog_translate(raster_rgb, "cogeo.tif", jpeg_profile, quiet=True)
        assert cog_validate("cogeo.tif")

        cog_translate(
            raster_rgb, "cogeo.tif", jpeg_profile, overview_level=0, quiet=True
        )
        assert cog_validate("cogeo.tif")

        # Change in rasterio 1.0.26
        # https://github.com/mapbox/rasterio/blob/master/CHANGES.txt#L43
        config = dict(GDAL_TIFF_OVR_BLOCKSIZE="1024")
        cog_translate(
            raster_big,
            "cogeo.tif",
            jpeg_profile,
            overview_level=1,
            config=config,
            quiet=True,
        )
        assert cog_validate("cogeo.tif")
Ejemplo n.º 46
0
    def test_configure_list_config(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            touch('dwave.conf')
            with mock.patch('dwave.cloud.config.homebase.site_config_dir_list',
                            lambda **kw: ['/system1', '/system2']):
                with mock.patch('dwave.cloud.config.homebase.user_config_dir',
                                lambda **kw: '/user'):
                    with mock.patch('os.path.exists', lambda *x: True):
                        # test --list-config-files
                        result = runner.invoke(
                            cli, ['configure', '--list-config-files'])
                        self.assertEqual(
                            result.output.strip(), '\n'.join([
                                '/system1/dwave.conf', '/system2/dwave.conf',
                                '/user/dwave.conf', './dwave.conf'
                            ]))

                        # test --list-system-paths
                        result = runner.invoke(
                            cli, ['configure', '--list-system-paths'])
                        self.assertEqual(
                            result.output.strip(), '\n'.join(
                                ['/system1/dwave.conf',
                                 '/system2/dwave.conf']))

                        # test --list-user-paths
                        result = runner.invoke(
                            cli, ['configure', '--list-user-paths'])
                        self.assertEqual(result.output.strip(),
                                         '/user/dwave.conf')

                        # test --list-local-paths
                        result = runner.invoke(
                            cli, ['configure', '--list-local-paths'])
                        self.assertEqual(result.output.strip(), './dwave.conf')
Ejemplo n.º 47
0
def test_remove_remote(managed_tmpdir):
    from hangar.remotes import RemoteInfo

    runner = CliRunner()
    with runner.isolated_filesystem():
        P = getcwd()
        repo = Repository(P, exists=False)
        try:
            res = runner.invoke(cli.init, ['--name', 'test', '--email', '*****@*****.**'], obj=repo)
            assert res.exit_code == 0

            res = runner.invoke(cli.add_remote, ['origin', 'localhost:50051'], obj=repo)
            assert res.exit_code == 0
            assert res.stdout == "RemoteInfo(name='origin', address='localhost:50051')\n"

            remote_list = repo.remote.list_all()
            assert remote_list == [RemoteInfo(name='origin', address='localhost:50051')]

            res = runner.invoke(cli.remove_remote, ['origin'], obj=repo)
            assert res.exit_code == 0
            assert res.stdout == "RemoteInfo(name='origin', address='localhost:50051')\n"
            assert repo.remote.list_all() == []
        finally:
            repo._env._close_environments()
Ejemplo n.º 48
0
def test_filter_alert_definitions(monkeypatch):
    get = MagicMock()
    get.return_value = [
        {
            'team': 'ZMON',
            'responsible_team': 'ZMON',
            'name': 'alert-1',
            'id': 1,
            'status': 'ACTIVE',
            'priority': 1,
            'last_modified': 1473418659294,
            'last_modified_by': 'user-1',
            'check_definition_id': 33
        },
        {
            'team': 'FANCY',
            'responsible_team': 'ZMON',
            'name': 'alert-2',
            'id': 2,
            'status': 'ACTIVE',
            'priority': 2,
            'last_modified': 1473418659294,
            'last_modified_by': 'user-2',
            'check_definition_id': 34
        },
    ]

    monkeypatch.setattr('zmon_cli.client.Zmon.get_alert_definitions', get)
    monkeypatch.setattr('zmon_cli.cmds.command.get_client', get_client)

    runner = CliRunner()

    with runner.isolated_filesystem():
        with open('test.yaml', 'w') as fd:
            yaml.dump({'url': 'foo', 'token': 123}, fd)

        result = runner.invoke(
            cli, ['-c', 'test.yaml', 'alert', 'f', 'team', 'ZMON'],
            catch_exceptions=False)

        out = result.output.rstrip()

        assert 'ZMON' in out
        assert 'alert-1' in out
        assert 'ago' in out
        assert 'Link' in out
        assert 'HIGH' in out

        assert 'FANCY' not in out
        assert 'alert-2' not in out
        assert 'MEDIUM' not in out

        result = runner.invoke(
            cli,
            ['-c', 'test.yaml', 'alert', 'f', 'check_definition_id', '34'],
            catch_exceptions=False)

        out = result.output.rstrip()

        assert 'ZMON' in out
        assert 'alert-1' not in out

        assert 'ago' in out
        assert 'Link' in out

        assert 'FANCY' in out
        assert 'alert-2' in out
        assert 'MEDIUM' in out
Ejemplo n.º 49
0
def cli_runner(tmp_path):
    runner = CliRunner()
    with runner.isolated_filesystem():
        yield partial(runner.invoke, env={"HOME": str(tmp_path)})
Ejemplo n.º 50
0
class TestVerdiDataBands(AiidaTestCase, DummyVerdiDataListable):
    """Testing verdi data bands."""
    @staticmethod
    def create_structure_bands():
        """Create bands structure object."""
        alat = 4.  # angstrom
        cell = [
            [
                alat,
                0.,
                0.,
            ],
            [
                0.,
                alat,
                0.,
            ],
            [
                0.,
                0.,
                alat,
            ],
        ]
        strct = StructureData(cell=cell)
        strct.append_atom(position=(0., 0., 0.), symbols='Fe')
        strct.append_atom(position=(alat / 2., alat / 2., alat / 2.),
                          symbols='O')
        strct.store()

        @calcfunction
        def connect_structure_bands(strct):  # pylint: disable=unused-argument
            alat = 4.
            cell = np.array([
                [alat, 0., 0.],
                [0., alat, 0.],
                [0., 0., alat],
            ])

            kpnts = KpointsData()
            kpnts.set_cell(cell)
            kpnts.set_kpoints([[0., 0., 0.], [0.1, 0.1, 0.1]])

            bands = BandsData()
            bands.set_kpointsdata(kpnts)
            bands.set_bands([[1.0, 2.0], [3.0, 4.0]])
            return bands

        bands = connect_structure_bands(strct)

        bands_isolated = BandsData()
        bands_isolated.store()

        # Create 2 groups and add the data to one of them
        g_ne = Group(label='non_empty_group')
        g_ne.store()
        g_ne.add_nodes(bands)
        g_ne.add_nodes(bands_isolated)

        g_e = Group(label='empty_group')
        g_e.store()

        return {
            DummyVerdiDataListable.NODE_ID_STR: bands.id,
            DummyVerdiDataListable.NON_EMPTY_GROUP_ID_STR: g_ne.id,
            DummyVerdiDataListable.EMPTY_GROUP_ID_STR: g_e.id
        }

    @classmethod
    def setUpClass(cls):  # pylint: disable=arguments-differ
        super().setUpClass()
        cls.ids = cls.create_structure_bands()

    def setUp(self):
        self.cli_runner = CliRunner()

    def test_bandsshowhelp(self):
        output = sp.check_output(['verdi', 'data', 'bands', 'show', '--help'])
        self.assertIn(b'Usage:', output,
                      'Sub-command verdi data bands show --help failed.')

    def test_bandlistshelp(self):
        output = sp.check_output(['verdi', 'data', 'bands', 'list', '--help'])
        self.assertIn(b'Usage:', output,
                      'Sub-command verdi data bands show --help failed.')

    def test_bandslist(self):
        self.data_listing_test(BandsData, 'FeO', self.ids)
        self.data_listing_test(BandsData, '<<NOT FOUND>>', self.ids)

    def test_bandslist_with_elements(self):
        options = ['-e', 'Fe']
        res = self.cli_runner.invoke(cmd_bands.bands_list,
                                     options,
                                     catch_exceptions=False)
        self.assertIn(b'FeO', res.stdout_bytes,
                      'The string "FeO" was not found in the listing')
        self.assertNotIn(
            b'<<NOT FOUND>>', res.stdout_bytes,
            'The string "<<NOT FOUND>>" should not in the listing')

    def test_bandexporthelp(self):
        output = sp.check_output(
            ['verdi', 'data', 'bands', 'export', '--help'])
        self.assertIn(b'Usage:', output,
                      'Sub-command verdi data bands export --help failed.')

    def test_bandsexport(self):
        options = [str(self.ids[DummyVerdiDataListable.NODE_ID_STR])]
        res = self.cli_runner.invoke(cmd_bands.bands_export,
                                     options,
                                     catch_exceptions=False)
        self.assertEqual(res.exit_code, 0,
                         'The command did not finish correctly')
        self.assertIn(
            b'[1.0, 3.0]', res.stdout_bytes,
            'The string [1.0, 3.0] was not found in the bands'
            'export')

    def test_bandsexport_single_kp(self):
        """
        Plot band for single k-point (issue #2462).
        """
        kpnts = KpointsData()
        kpnts.set_kpoints([[0., 0., 0.]])

        bands = BandsData()
        bands.set_kpointsdata(kpnts)
        bands.set_bands([[1.0, 2.0]])
        bands.store()

        # matplotlib
        options = [str(bands.id), '--format', 'mpl_singlefile']
        res = self.cli_runner.invoke(cmd_bands.bands_export,
                                     options,
                                     catch_exceptions=False)
        self.assertIn(
            b'p.scatter', res.stdout_bytes,
            'The string p.scatter was not found in the bands mpl export')

        # gnuplot
        with self.cli_runner.isolated_filesystem():
            options = [str(bands.id), '--format', 'gnuplot', '-o', 'bands.gnu']
            self.cli_runner.invoke(cmd_bands.bands_export,
                                   options,
                                   catch_exceptions=False)
            with open('bands.gnu', 'r') as gnu_file:
                res = gnu_file.read()
                self.assertIn(
                    'vectors nohead', res,
                    'The string "vectors nohead" was not found in the gnuplot script'
                )
Ejemplo n.º 51
0
def test_insert_files():
    runner = CliRunner()
    with runner.isolated_filesystem():
        tmpdir = pathlib.Path(".")
        db_path = str(tmpdir / "files.db")
        (tmpdir / "one.txt").write_text("This is file one", "utf-8")
        (tmpdir / "two.txt").write_text("Two is shorter", "utf-8")
        (tmpdir / "nested").mkdir()
        (tmpdir / "nested" / "three.txt").write_text("Three is nested",
                                                     "utf-8")
        coltypes = (
            "name",
            "path",
            "fullpath",
            "sha256",
            "md5",
            "mode",
            "content",
            "mtime",
            "ctime",
            "mtime_int",
            "ctime_int",
            "mtime_iso",
            "ctime_iso",
            "size",
        )
        cols = []
        for coltype in coltypes:
            cols += ["-c", "{}:{}".format(coltype, coltype)]
        result = runner.invoke(
            cli.cli,
            ["insert-files", db_path, "files",
             str(tmpdir)] + cols + ["--pk", "path"],
            catch_exceptions=False,
        )
        assert result.exit_code == 0, result.stdout
        db = Database(db_path)
        rows_by_path = {r["path"]: r for r in db["files"].rows}
        one, two, three = (
            rows_by_path["one.txt"],
            rows_by_path["two.txt"],
            rows_by_path[os.path.join("nested", "three.txt")],
        )
        assert {
            "content": b"This is file one",
            "md5": "556dfb57fce9ca301f914e2273adf354",
            "name": "one.txt",
            "path": "one.txt",
            "sha256":
            "e34138f26b5f7368f298b4e736fea0aad87ddec69fbd04dc183b20f4d844bad5",
            "size": 16,
        }.items() <= one.items()
        assert {
            "content": b"Two is shorter",
            "md5": "f86f067b083af1911043eb215e74ac70",
            "name": "two.txt",
            "path": "two.txt",
            "sha256":
            "9368988ed16d4a2da0af9db9b686d385b942cb3ffd4e013f43aed2ec041183d9",
            "size": 14,
        }.items() <= two.items()
        assert {
            "content": b"Three is nested",
            "md5": "12580f341781f5a5b589164d3cd39523",
            "name": "three.txt",
            "path": os.path.join("nested", "three.txt"),
            "sha256":
            "6dd45aaaaa6b9f96af19363a92c8fca5d34791d3c35c44eb19468a6a862cc8cd",
            "size": 15,
        }.items() <= three.items()
        # Assert the other int/str/float columns exist and are of the right types
        expected_types = {
            "ctime": float,
            "ctime_int": int,
            "ctime_iso": str,
            "mtime": float,
            "mtime_int": int,
            "mtime_iso": str,
            "mode": int,
            "fullpath": str,
        }
        for colname, expected_type in expected_types.items():
            for row in (one, two, three):
                assert isinstance(row[colname], expected_type)
Ejemplo n.º 52
0
class TestParamsAdd(TestCase):
    def setUp(self) -> None:
        self.runner = CliRunner()
        self.add = add

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_missing_key_parameters(
            self, mock_ensure_object):
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.add)
            self.assertEqual(result.exit_code, 2)
            self.assertEqual(
                result.output, "Usage: add [OPTIONS]\n"
                "Try 'add --help' for help.\n"
                "\n"
                "Error: Missing option '--param' / '-p'.\n")

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_skip_user_prompt_default_values(
            self, mock_ensure_object):
        params = {'parameter': 'test'}
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.add,
                                        ['-p', params['parameter'], '-y'])
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Adding parameter {params['parameter']}\n"
                f"Adding {params['parameter']} value None for gameta\n"
                f"Adding {params['parameter']} value None for GitPython\n"
                f"Adding {params['parameter']} value None for gitdb\n"
                f"Successfully added parameter {params['parameter']} to .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                params['parameter']: None,
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                params['parameter']: None,
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                params['parameter']: None,
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_skip_user_prompt_user_provided_default_value(
            self, mock_ensure_object):
        params = {'parameter': 'test', 'value': 'hello_world'}
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(
                self.add,
                ['-p', params['parameter'], '-y', '-v', params['value']])
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Adding parameter {params['parameter']}\n"
                f"Adding {params['parameter']} value {params['value']} for gameta\n"
                f"Adding {params['parameter']} value {params['value']} for GitPython\n"
                f"Adding {params['parameter']} value {params['value']} for gitdb\n"
                f"Successfully added parameter {params['parameter']} to .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                params['parameter']: params['value'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                params['parameter']: params['value'],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                params['parameter']: params['value'],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_skip_user_prompt_default_type_not_in_choice(
            self, mock_ensure_object):
        params = {'parameter': 'test', 'type': 'test'}
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(
                self.add,
                ['-p', params['parameter'], '-y', '-t', params['type']])
            self.assertEqual(result.exit_code, 2)
            self.assertEqual(
                result.output, "Usage: add [OPTIONS]\n"
                "Try 'add --help' for help.\n"
                "\n"
                "Error: Invalid value for '--type' / '-t': invalid choice: test. "
                "(choose from int, float, str, bool, dict, list)\n")

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_user_prompt_all_value_prompted(
            self, mock_ensure_object):
        params = {
            'parameter': 'test',
            'value': 'hello_world',
            'user_prompt': ['gameta', 'GitPython', 'gitdb']
        }
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(
                self.add, ['-p', params['parameter'], '-v', params['value']],
                input='\n'.join(params['user_prompt']))
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Adding parameter {params['parameter']}\n"
                f"Please enter the parameter value for repository gameta or >* to skip [hello_world]: "
                f"{params['user_prompt'][0]}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][0]} for gameta\n"
                f"Please enter the parameter value for repository GitPython or >* to skip [hello_world]: "
                f"{params['user_prompt'][1]}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][1]} for GitPython\n"
                f"Please enter the parameter value for repository gitdb or >* to skip [hello_world]: "
                f"{params['user_prompt'][2]}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][2]} for gitdb\n"
                f"Successfully added parameter {params['parameter']} to .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                params['parameter']: params['user_prompt'][1],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                params['parameter']: params['user_prompt'][0],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                params['parameter']: params['user_prompt'][2],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_user_prompt_skipping_with_user_provided_default_value(
            self, mock_ensure_object):
        params = {
            'parameter': 'test',
            'value': 'hello_world',
            'user_prompt': ['gameta', '>*', 'gitdb']
        }
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(
                self.add, ['-p', params['parameter'], '-v', params['value']],
                input='\n'.join(params['user_prompt']))
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Adding parameter {params['parameter']}\n"
                f"Please enter the parameter value for repository gameta or >* to skip [hello_world]: "
                f"{params['user_prompt'][0]}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][0]} for gameta\n"
                f"Please enter the parameter value for repository GitPython or >* to skip [hello_world]: "
                f"{params['user_prompt'][1]}\n"
                f"Skip token was entered, defaulting to default value {params['value']}\n"
                f"Adding {params['parameter']} value {params['value']} for GitPython\n"
                f"Please enter the parameter value for repository gitdb or >* to skip [hello_world]: "
                f"{params['user_prompt'][2]}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][2]} for gitdb\n"
                f"Successfully added parameter {params['parameter']} to .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                params['parameter']: params['value'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                params['parameter']: params['user_prompt'][0],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                params['parameter']: params['user_prompt'][2],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_user_prompt_complex_user_input(
            self, mock_ensure_object):
        params = {
            'parameter':
            'test',
            'value':
            'hello_world',
            'type':
            'dict',
            'user_prompt': [{
                'a': [1, 2, 3]
            }, {
                'a': [4, 5, 6]
            }, {
                'a': [1, 6, 7],
                'c': [4, 2, 8]
            }]
        }
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(
                self.add, [
                    '-p', params['parameter'], '-v', params['value'], '--type',
                    params['type']
                ],
                input='\n'.join([json.dumps(p)
                                 for p in params['user_prompt']]))
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Adding parameter {params['parameter']}\n"
                f"Please enter the parameter value for repository gameta or >* to skip [hello_world]: "
                f"{json.dumps(params['user_prompt'][0])}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][0]} for gameta\n"
                f"Please enter the parameter value for repository GitPython or >* to skip [hello_world]: "
                f"{json.dumps(params['user_prompt'][1])}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][1]} for GitPython\n"
                f"Please enter the parameter value for repository gitdb or >* to skip [hello_world]: "
                f"{json.dumps(params['user_prompt'][2])}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][2]} for gitdb\n"
                f"Successfully added parameter {params['parameter']} to .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                params['parameter']: params['user_prompt'][1],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                params['parameter']: params['user_prompt'][0],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                params['parameter']: params['user_prompt'][2],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_add_parameters_user_prompt_user_input_does_not_match_required_type(
            self, mock_ensure_object):
        params = {
            'parameter':
            'test',
            'value':
            'hello_world',
            'type':
            'dict',
            'user_prompt': [{
                'a': [1, 2, 3]
            }, ['a', [4, 5, 6]], {
                'a': [1, 6, 7],
                'c': [4, 2, 8]
            }]
        }
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(
                self.add, [
                    '-p', params['parameter'], '-v', params['value'], '--type',
                    params['type']
                ],
                input='\n'.join([json.dumps(p)
                                 for p in params['user_prompt']]))
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Adding parameter {params['parameter']}\n"
                f"Please enter the parameter value for repository gameta or >* to skip [hello_world]: "
                f"{json.dumps(params['user_prompt'][0])}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][0]} for gameta\n"
                f"Please enter the parameter value for repository GitPython or >* to skip [hello_world]: "
                f"{json.dumps(params['user_prompt'][1])}\n"
                f"Value {params['user_prompt'][1]} (type: {type(params['user_prompt'][1])}) entered is not the "
                f"required type {dict}, defaulting to default value {params['value']}\n"
                f"Adding {params['parameter']} value {params['value']} for GitPython\n"
                f"Please enter the parameter value for repository gitdb or >* to skip [hello_world]: "
                f"{json.dumps(params['user_prompt'][2])}\n"
                f"Adding {params['parameter']} value {params['user_prompt'][2]} for gitdb\n"
                f"Successfully added parameter {params['parameter']} to .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                params['parameter']: params['value'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                params['parameter']: params['user_prompt'][0],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                params['parameter']: params['user_prompt'][2],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })
Ejemplo n.º 53
0
def runner():
    cli_runner = CliRunner(mix_stderr=False)
    with cli_runner.isolated_filesystem():
        yield cli_runner
Ejemplo n.º 54
0
 def test_printing_of_version(self):
     runner = CliRunner()
     with runner.isolated_filesystem():
         result = runner.invoke(cffconvert_cli, ["--version"])
     self.assertTrue(result.exit_code == 0)
     self.assertEqual(result.output, "1.4.0-alpha0\n")
Ejemplo n.º 55
0
def test_cli(script_info):
    """Test npm CLI."""

    deps = {"bootstrap": "3.0.0"}
    bundle = Bundle(NpmBundle(npm=deps))

    runner = CliRunner()
    app = script_info.load_app()
    assets = app.extensions['invenio-assets']
    assets.env.register('test1', bundle)
    assert len(assets.env) == 1

    expected = {
        "name": app.name,
        "dependencies": {
            "bootstrap": "3.0.0",
        },
        "version": "",
    }

    # Test default output
    with runner.isolated_filesystem():
        result = runner.invoke(npm, obj=script_info)
        assert result.exit_code == 0

        filepath = os.path.join(app.static_folder, 'package.json')
        assert os.path.exists(filepath)

        with open(filepath) as f:
            package_json = json.loads(f.read())
        assert package_json == expected

    # Test writing package.json file.
    with runner.isolated_filesystem():
        result = runner.invoke(npm, ['-o', 'package.json'], obj=script_info)
        assert result.exit_code == 0

        filepath = os.path.join('package.json')
        assert os.path.exists(filepath)

        with open('package.json') as f:
            package_json = json.loads(f.read())
        assert package_json == expected

    # Test merging a base another file.
    newdep = {'font-awesome': '4.0'}
    expected['dependencies'].update(newdep)
    with runner.isolated_filesystem():
        with open('base.json', 'wt') as f:
            f.write(json.dumps({'dependencies': newdep}))

        result = runner.invoke(npm, ['-i', 'base.json', '-o', 'package.json'],
                               obj=script_info)
        assert result.exit_code == 0

        filepath = os.path.join('package.json')
        assert os.path.exists(filepath)

        with open('package.json') as f:
            package_json = json.loads(f.read())
        assert package_json == expected
Ejemplo n.º 56
0
 def test_printing_of_help(self):
     runner = CliRunner()
     with runner.isolated_filesystem():
         result = runner.invoke(cffconvert_cli, ["--help"])
     self.assertTrue(result.exit_code == 0)
     self.assertTrue(result.output[:6] == "Usage:")
def test_version():
    runner = CliRunner()
    with runner.isolated_filesystem():
        result = runner.invoke(cli, ["--version"])
        assert result.exit_code == 0
        assert result.output.startswith("cli, version ")
Ejemplo n.º 58
0
def __test_deploy_and_upgrade(cloud, region):
    logging.basicConfig(format='%(message)s', level=logging.DEBUG)

    email = os.environ['GIGALIXIR_EMAIL']
    password = os.environ['GIGALIXIR_PASSWORD']
    runner = CliRunner()

    with runner.isolated_filesystem():
        os.environ['HOME'] = os.getcwd()
        result = runner.invoke(gigalixir.cli, ['login', '--email=%s' % email], input="%s\ny\n" % password)
        assert result.exit_code == 0
        gigalixir.shell.cast("git clone https://github.com/gigalixir/gigalixir-getting-started.git")
        with cd("gigalixir-getting-started"):
            result = runner.invoke(gigalixir.cli, ['create', '--cloud=%s' % cloud, '--region=%s' % region])
            assert result.exit_code == 0
            app_name = result.output.rstrip()
            gigalixir.shell.cast("git push gigalixir master")

            logging.info('Completed Deploy.')
            start_time = timeit.default_timer()
            url = 'https://%s.gigalixirapp.com/' % app_name
            for i in range(30):
                try:
                    logging.info('Attempt: %s/30: Checking %s' % (i, url))
                    r = requests.get(url)
                    if r.status_code != 200:
                        # wait 5 seconds
                        logging.info('Received %s' % r.status_code)
                        logging.info('Waiting 5 seconds to try again.')
                        time.sleep(5)
                    else:
                        logging.info('Pass.')
                        break
                except requests.exceptions.ConnectionError as e:
                    # wait 5 seconds
                    logging.info('ConnectionError: %s' % e)
                    logging.info('Waiting 5 seconds to try again.')
                    time.sleep(5)
            else:
                logging.info('Exhausted retries. Be sure to scale down your app manually and other necessary cleanup since we are aborting now.')
                assert False

            elapsed = timeit.default_timer() - start_time
            logging.info("Elapsed time: %s" % elapsed)

            # check status
            result = runner.invoke(gigalixir.cli, ['ps'])
            assert result.exit_code == 0
            status = json.loads(result.output)
            assert status["replicas_desired"] == 1
            assert status["replicas_running"] == 1

            # set a config
            result = runner.invoke(gigalixir.cli, ['config:set', "FOO=foo"])
            assert result.exit_code == 0

            # get configs
            result = runner.invoke(gigalixir.cli, ['config'])
            assert result.exit_code == 0
            configs = json.loads(result.output)
            assert configs == {"FOO": "foo"}

            # delete the config
            result = runner.invoke(gigalixir.cli, ['config:unset', "FOO"])
            assert result.exit_code == 0

            # get configs
            result = runner.invoke(gigalixir.cli, ['config'])
            assert result.exit_code == 0
            configs = json.loads(result.output)
            assert configs == {}

            # hot upgrade
            gigalixir.shell.cast("""git rebase origin/v0.0.2""")
            subprocess.check_call(["git", "-c", "http.extraheader=GIGALIXIR-HOT:true","push","gigalixir","master"])

            logging.info('Completed Hot Upgrade.')
            start_time = timeit.default_timer()
            url = 'https://%s.gigalixirapp.com/' % app_name
            for i in range(30):
                try:
                    logging.info('Attempt: %s/30: Checking %s' % (i, url))
                    r = requests.get(url)
                    if r.status_code != 200:
                        # wait 5 seconds
                        logging.info('Received %s' % r.status_code)
                        logging.info('Waiting 5 seconds to try again.')
                        time.sleep(5)
                    else:
                        if "0.0.2" not in r.text:
                            # wait 5 seconds
                            logging.info('0.0.2 not found.')
                            logging.info('Waiting 5 seconds to try again.')
                            time.sleep(5)
                        else:
                            logging.info('Pass.')
                            break
                except requests.exceptions.ConnectionError as e:
                    # wait 5 seconds
                    logging.info('ConnectionError: %s' % e)
                    logging.info('Waiting 5 seconds to try again.')
                    time.sleep(5)
            else:
                logging.info('Exhausted retries. Be sure to scale down your app manually and other necessary cleanup since we are aborting now.')
                assert False
            elapsed = timeit.default_timer() - start_time
            logging.info("Elapsed time: %s" % elapsed)

            # scale down to 0
            result = runner.invoke(gigalixir.cli, ['ps:scale', '--replicas=0'])
            assert result.exit_code == 0

            # check status
            result = runner.invoke(gigalixir.cli, ['ps'])
            assert result.exit_code == 0
            status = json.loads(result.output)
            assert status["replicas_desired"] == 0
            assert status["replicas_running"] == 0
Ejemplo n.º 59
0
class CliTestCase(TestCase):

    def setUp(self):  # noqa
        self._runner = CliRunner()
        self._home = mkdtemp()
        self._old_config = os.environ.get(PLANEMO_CONFIG_ENV_PROP, None)
        os.environ[PLANEMO_CONFIG_ENV_PROP] = self.planemo_yaml_path

    def tearDown(self):  # noqa
        if self._old_config:
            os.environ[PLANEMO_CONFIG_ENV_PROP] = self._old_config
        else:
            del os.environ[PLANEMO_CONFIG_ENV_PROP]
        shutil.rmtree(self._home)

    @property
    def planemo_yaml_path(self):
        return os.path.join(self._home, ".planemo.yml")

    @property
    def _cli(self):
        return cli

    def _isolate(self):
        return self._runner.isolated_filesystem()

    def _invoke(self, command_list):
        planemo_cli = self._cli.planemo
        return self._runner.invoke(planemo_cli, command_list)

    def _check_exit_code(self, command_list, exit_code=0):
        expected_exit_code = exit_code
        result = self._invoke(command_list)
        print(result.output)
        result_exit_code = result.exit_code
        if result_exit_code != expected_exit_code:
            message = EXIT_CODE_MESSAGE % (
                " ".join(command_list),
                result_exit_code,
                expected_exit_code,
                result.output,
            )
            if result.exception:
                message += " Exception [%s]." % str(result.exception)
            raise AssertionError(message)
        return result

    @contextlib.contextmanager
    def _isolate_repo(self, name):
        with self._isolate() as f:
            self._copy_repo(name, f)
            yield f

    def _copy_repo(self, name, dest):
        repo = os.path.join(TEST_REPOS_DIR, name)
        io.shell("cp -r '%s/.' '%s'" % (repo, dest))

    @property
    def test_context(self):
        context = cli.Context()
        context.planemo_directory = "/tmp/planemo-test-workspace"
        return context
Ejemplo n.º 60
0
class TestParamsDelete(TestCase):
    def setUp(self) -> None:
        self.runner = CliRunner()
        self.delete = delete

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_delete_parameters_missing_key_parameters(
            self, mock_ensure_object):
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.delete)
            self.assertEqual(result.exit_code, 2)
            self.assertEqual(
                result.output, "Usage: delete [OPTIONS]\n"
                "Try 'delete --help' for help.\n"
                "\n"
                "Error: Missing option '--param' / '-p'.\n")

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_delete_parameters_parameter_does_not_exist(
            self, mock_ensure_object):
        params = {'parameter': 'test'}
        with self.runner.isolated_filesystem() as f:
            copyfile(join(dirname(__file__), 'data', '.meta_other_repos'),
                     join(f, '.meta'))
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.delete,
                                        ['-p', params['parameter']])
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Deleting parameter {params['parameter']}\n"
                f"Successfully deleted parameter {params['parameter']} from .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_delete_parameters_all_parameters_deleted(
            self, mock_ensure_object):
        params = {'parameter': 'test'}
        with self.runner.isolated_filesystem() as f:
            with open(join(dirname(__file__), 'data', '.meta_other_repos'),
                      'r') as m1:
                output = json.load(m1)
                with open(join(f, '.meta'), 'w+') as m2:
                    output['projects']['gameta'].update(
                        {"test": {
                            'a': [1, 2, 3]
                        }})
                    output['projects']['GitPython'].update(
                        {'test': {
                            'a': [4, 5, 6]
                        }})
                    output['projects']['gitdb'].update(
                        {'test': {
                            'a': [1, 6, 7],
                            'c': [4, 2, 8]
                        }})
                    json.dump(output, m2)
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.delete,
                                        ['-p', params['parameter']])
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Deleting parameter {params['parameter']}\n"
                f"Successfully deleted parameter {params['parameter']} from .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_delete_parameters_partial_parameter_deleted(
            self, mock_ensure_object):
        params = {'parameter': 'test'}
        with self.runner.isolated_filesystem() as f:
            with open(join(dirname(__file__), 'data', '.meta_other_repos'),
                      'r') as m1:
                output = json.load(m1)
                with open(join(f, '.meta'), 'w+') as m2:
                    output['projects']['gameta'].update(
                        {"test": {
                            'a': [1, 2, 3]
                        }})
                    output['projects']['gitdb'].update(
                        {'test': {
                            'a': [1, 6, 7],
                            'c': [4, 2, 8]
                        }})
                    json.dump(output, m2)
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.delete,
                                        ['-p', params['parameter']])
            self.assertEqual(result.exit_code, 0)
            self.assertEqual(
                result.output, f"Deleting parameter {params['parameter']}\n"
                f"Successfully deleted parameter {params['parameter']} from .meta file\n"
            )
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                '__metarepo__': False
                            }
                        }
                    })

    @patch('gameta.cli.click.Context.ensure_object')
    def test_params_delete_parameters_attempting_to_delete_reserved_parameters(
            self, mock_ensure_object):
        params = {'parameter': 'url'}
        with self.runner.isolated_filesystem() as f:
            with open(join(dirname(__file__), 'data', '.meta_other_repos'),
                      'r') as m1:
                output = json.load(m1)
                with open(join(f, '.meta'), 'w+') as m2:
                    output['projects']['gameta'].update(
                        {"test": {
                            'a': [1, 2, 3]
                        }})
                    output['projects']['GitPython'].update(
                        {'test': {
                            'a': [4, 5, 6]
                        }})
                    output['projects']['gitdb'].update(
                        {'test': {
                            'a': [1, 6, 7],
                            'c': [4, 2, 8]
                        }})
                    json.dump(output, m2)
            context = GametaContext()
            context.project_dir = f
            context.load()
            mock_ensure_object.return_value = context
            result = self.runner.invoke(self.delete,
                                        ['-p', params['parameter']])
            self.assertEqual(result.exit_code, 1)
            self.assertEqual(
                result.output,
                f"Error: Parameter {params['parameter']} is a reserved parameter "
                f"['url', 'path', 'tags', '__metarepo__']\n")
            with open(join(f, '.meta'), 'r') as m:
                self.assertEqual(
                    json.load(m), {
                        'projects': {
                            'GitPython': {
                                'path': 'GitPython',
                                'tags': ['a', 'b', 'c'],
                                'url':
                                'https://github.com/gitpython-developers/GitPython.git',
                                'test': {
                                    'a': [4, 5, 6]
                                },
                                '__metarepo__': False
                            },
                            'gameta': {
                                'path': '.',
                                'tags': ['metarepo'],
                                'url':
                                '[email protected]:genius-systems/gameta.git',
                                'test': {
                                    'a': [1, 2, 3]
                                },
                                '__metarepo__': True
                            },
                            'gitdb': {
                                'path': 'core/gitdb',
                                'tags': ['a', 'c', 'd'],
                                'url':
                                'https://github.com/gitpython-developers/gitdb.git',
                                'test': {
                                    'a': [1, 6, 7],
                                    'c': [4, 2, 8]
                                },
                                '__metarepo__': False
                            }
                        }
                    })