def test_location_not_recognized_error(self, monkeypatch, tmpdir): """Tests that an error is raised if the configuration file is supposed to be created in an unrecognized directory.""" with pytest.raises(conf.ConfigurationError, match="This location is not recognized."): conf.store_account(authentication_token, filename="config.toml", location="UNRECOGNIZED_LOCATION", **DEFAULT_KWARGS)
def test_nested_directory_is_created(self, monkeypatch, tmpdir): recursive_dir = tmpdir.join(".new_dir", "new_dir_again") with monkeypatch.context() as m: m.setattr(conf, "user_config_dir", lambda *args: recursive_dir) conf.store_account(authentication_token, filename="config.toml", location="user_config", **DEFAULT_KWARGS) filepath = os.path.join(recursive_dir, "config.toml") result = toml.load(filepath) assert result == EXPECTED_CONFIG
def test_non_existing_directory_does_not_raise_file_not_found_error( self, monkeypatch, tmpdir): """Tests that an error is raised if the configuration file is supposed to be created in a non-existing directory when using user_config_dir and if os.makedirs does not create the directory.""" with monkeypatch.context() as m: m.setattr(conf, "user_config_dir", lambda *args: tmpdir.join("new_dir")) conf.store_account(authentication_token, filename="config.toml", location="user_config", **DEFAULT_KWARGS)
def test_global(self, monkeypatch, tmpdir): """Tests that the functions integrate correctly when storing account globally.""" with monkeypatch.context() as m: m.setattr(conf, "user_config_dir", lambda *args: tmpdir) conf.store_account(authentication_token, filename="config.toml", location="user_config", **DEFAULT_KWARGS) filepath = tmpdir.join("config.toml") result = toml.load(filepath) assert result == EXPECTED_CONFIG
def test_non_existing_directory_without_makedirs_raises_error( self, monkeypatch, tmpdir): """Tests that an error is raised if the configuration file is supposed to be created in a non-existing directory when using user_config_dir and if os.makedirs does not create the directory.""" with monkeypatch.context() as m: m.setattr(os, "makedirs", lambda a, **kwargs: None) m.setattr(conf, "user_config_dir", lambda *args: tmpdir.join("new_dir")) with pytest.raises(FileNotFoundError, match="No such file or directory"): conf.store_account(authentication_token, filename="config.toml", location="user_config", **DEFAULT_KWARGS)
def test_global_config_created(self, monkeypatch, tmpdir): """Tests that a configuration file was created in the user configuration directory for Strawberry Fields.""" mock_save_config_file = MockSaveConfigToFile() assert mock_save_config_file.config is None assert mock_save_config_file.path is None with monkeypatch.context() as m: m.setattr(os, "getcwd", lambda: "NotTheCorrectDir") m.setattr(conf, "user_config_dir", lambda *args: tmpdir) m.setattr(conf, "create_config", mock_create_config) m.setattr(conf, "save_config_to_file", lambda a, b: mock_save_config_file.update(a, b)) conf.store_account(authentication_token, filename="config.toml", location="user_config", **DEFAULT_KWARGS) assert mock_save_config_file.config == EXPECTED_CONFIG assert mock_save_config_file.path == tmpdir.join("config.toml")
def configure(args): r"""An auxiliary function for configuring the API connection to the Xanadu cloud platform. Can be used to simply save the authentication token with default configuration options. Alternatively, a wizard is provided for full configurability. See more details regarding Strawberry Fields configuration and available configuration options on the :doc:`/code/sf_configuration` page. Args: args (ArgumentParser): arguments that were specified on the command line stored as attributes in an argument parser object """ if args.token: kwargs = {"authentication_token": args.token} else: kwargs = configuration_wizard() if args.local: store_account(**kwargs, location="local") else: store_account(**kwargs)