def test_upload_discover_pathgiven_ok(tmp_path): """Discover charm name/path, indicated path ok.""" charm_file = tmp_path / 'testfile.charm' charm_file.touch() name, path = UploadCommand('group')._discover_charm(charm_file) assert name == 'testfile' assert path == charm_file
def test_upload_discover_pathgiven_missing(tmp_path): """Discover charm name/path, the indicated path is not there.""" with pytest.raises(CommandError) as cm: UploadCommand('group')._discover_charm( pathlib.Path('not_really_there.charm')) assert str( cm.value ) == "Can't access the indicated charm file: 'not_really_there.charm'"
def test_upload_discover_default_no_metadata(tmp_path): """Discover charm name/path, no metadata file to get info.""" with patch('charmcraft.commands.store.get_name_from_metadata') as mock: mock.return_value = None with pytest.raises(CommandError) as cm: UploadCommand('group')._discover_charm(None) assert str(cm.value) == ( "Can't access name in 'metadata.yaml' file. The 'upload' command needs to be executed in " "a valid project's directory, or point to a charm file with the --charm-file option.")
def test_upload_discover_pathgiven_home_expanded(tmp_path): """Discover charm name/path, home-expand the indicated path.""" fake_home = tmp_path / 'homedir' fake_home.mkdir() charm_file = fake_home / 'testfile.charm' charm_file.touch() with patch.dict(os.environ, {'HOME': str(fake_home)}): name, path = UploadCommand('group')._discover_charm(pathlib.Path('~/testfile.charm')) assert name == 'testfile' assert path == charm_file
def test_upload_call_error(caplog, store_mock): """Simple upload but with a response indicating an error.""" caplog.set_level(logging.INFO, logger="charmcraft.commands") store_response = Uploaded(ok=False, status=400, revision=None) store_mock.upload.return_value = store_response args = Namespace(charm_file='whatever-cmd-arg') with patch('charmcraft.commands.store.UploadCommand._discover_charm') as mock_discover: mock_discover.return_value = ('discovered-name', 'discovered-path') UploadCommand('group').run(args) expected = "Upload failed: got status 400" assert [expected] == [rec.message for rec in caplog.records]
def test_upload_discover_default_no_charm_file(tmp_path, monkeypatch): """Discover charm name/path, the metadata indicates a not accesible.""" monkeypatch.chdir(tmp_path) # fake the metadata to point to a missing file metadata_data = {'name': 'testcharm'} metadata_file = tmp_path / 'metadata.yaml' metadata_raw = yaml.dump(metadata_data).encode('ascii') with metadata_file.open('wb') as fh: fh.write(metadata_raw) with pytest.raises(CommandError) as cm: UploadCommand('group')._discover_charm(None) assert str(cm.value) == ( "Can't access charm file {!r}. You can indicate a charm file with " "the --charm-file option.".format(str(tmp_path / 'testcharm.charm')))
def test_upload_discover_default_ok(tmp_path, monkeypatch): """Discover charm name/path, default to get info from metadata, ok.""" monkeypatch.chdir(tmp_path) # touch the charm file charm_file = tmp_path / 'testcharm.charm' charm_file.touch() # fake the metadata to point to that file with patch('charmcraft.commands.store.get_name_from_metadata') as mock: mock.return_value = 'testcharm' name, path = UploadCommand('group')._discover_charm(None) assert name == 'testcharm' assert path == charm_file
def test_upload_call_ok(caplog, store_mock): """Simple upload, success result.""" caplog.set_level(logging.INFO, logger="charmcraft.commands") store_response = Uploaded(ok=True, status=200, revision=7) store_mock.upload.return_value = store_response args = Namespace(charm_file='whatever-cmd-arg') with patch('charmcraft.commands.store.UploadCommand._discover_charm') as mock_discover: mock_discover.return_value = ('discovered-name', 'discovered-path') UploadCommand('group').run(args) # check it called self discover helper with correct args mock_discover.assert_called_once_with('whatever-cmd-arg') assert store_mock.mock_calls == [ call.upload('discovered-name', 'discovered-path') ] expected = "Revision 7 of 'discovered-name' created" assert [expected] == [rec.message for rec in caplog.records]
def test_upload_discover_pathgiven_not_a_file(tmp_path): """Discover charm name/path, the indicated path is not a file.""" with pytest.raises(CommandError) as cm: UploadCommand('group')._discover_charm(tmp_path) assert str(cm.value) == "The indicated charm is not a file: {!r}".format(str(tmp_path))