def test_from_file_exists(self, mock_isfile): data = '{"hello":"there"}' m = mock_open(read_data=data) path = "./this/is/a/test" with patch("chromie.config.open", m): result = Config.from_file(path) m.assert_called_once_with(path, "r") self.assertEqual(result.data, json.loads(data))
def test_set_new_values(self, mock_file): m = mock_open() path = "./this/is/a/test.json" with patch("chromie.config.open", m): config = Config.from_file(path) with patch("chromie.config.open", m): config.set_values(hello="there") self.assertEqual(config.data, {"hello": "there"})
def test_from_file_does_not_exist(self, mock_isfile): m = mock_open() path = "./this/is/a/test" with patch("chromie.config.open", m, create=True): result = Config.from_file(path) m.assert_called_once_with(path, "w") handle = m() handle.write.assert_called_once_with("") self.assertEqual(result.data, {})
def test_update_values(self, mock_file): m = mock_open(read_data=json.dumps({"hello": "there"})) path = "./this/is/a/test.json" with patch("chromie.config.open", m): config = Config.from_file(path) with patch("chromie.config.open", m): config.set_values(hi="again") test_case = {"hello": "there", "hi": "again"} self.assertEqual(config.data, test_case)
def config(args): filepath = os.path.abspath(args.filepath) dot_chromie = os.path.join(filepath, ".chromie") if not os.path.isdir(dot_chromie): os.mkdir(dot_chromie) config_file = Config.from_file( os.path.join(filepath, ".chromie/settings.json")) data = {args.name: args.value} config_file.set_values(**data)
def publish(args): root = os.path.abspath(args.filepath) dist = os.path.join(root, "dist") dot_chromie = os.path.join(root, ".chromie") if not os.path.isdir(dot_chromie): raise SystemExit(Settings.MISSING) config_file = Config.from_file(os.path.join(dot_chromie, "settings.json")) try: with GoogleWebStore.session( config_file.data["email"], credentials=config_file.data ) as session: try: extension_id = session.publish(config_file.data["extension_id"]) print(Publish.SUCCESS) except GoogleWebStoreError as e: print( Publish.UNSUCCESSFUL, e, sep="\n", ) except AuthenticationError: print(Settings.INVALID)
def upload(args): root = os.path.abspath(args.filepath) dist = os.path.join(root, "dist") dot_chromie = os.path.join(root, ".chromie") if not os.path.isdir(dot_chromie): raise SystemExit(Settings.MISSING) archive = get_latest_version(dist) config_file = Config.from_file(os.path.join(dot_chromie, "settings.json")) try: with GoogleWebStore.session(config_file.data["email"], credentials=config_file.data) as session: try: extension_id = session.upload(archive) data = {"extension_id": extension_id} config_file.set_values(**data) except GoogleWebStoreError as e: print(e) except AuthenticationError: print(Settings.INVALID)