def command(args): # Create our own tracking instance here because this command might write # the configuration file. is_write_action = args.setting in [Mode.TRACK, Mode.IGNORE, Mode.RESET] if args.shell.is_envprobe_capable: local_config_file = ConfigurationFile( os.path.join(args.shell.configuration_directory, get_tracking_file_name()), VariableTracking.config_schema_local, read_only=args.scope != Scope.LOCAL or (args.scope == Scope.LOCAL and not is_write_action) ) else: local_config_file = None global_config_file = ConfigurationFile( os.path.join(get_configuration_directory(), get_tracking_file_name()), VariableTracking.config_schema_global, read_only=args.scope != Scope.GLOBAL or (args.scope == Scope.GLOBAL and not is_write_action) ) tracker = VariableTracking(global_config_file, local_config_file) if args.default: return _handle_default(tracker, args) if args.setting == Mode.QUERY: return _handle_querying_variable(tracker, args) return _handle_setting_variable(tracker, args)
def test_save_default(tmp): c = ConfigurationFile("test.json", {"Default": True}) c.save() assert (os.path.isfile("test.json")) with open("test.json", 'r') as f: data = json.load(f) assert (data == {"Default": True})
def test_load_values(tmp): with open("test.json", 'w') as f: json.dump({"Default": False, "str": "Test string"}, f) c = ConfigurationFile("test.json", {"Default": True}) c.load() assert (len(c) == 2) assert (c["Default"] is False) assert (c["str"] == "Test string")
def test_load_empty(tmp): c = ConfigurationFile("test.json") assert (not c._data) assert (len(c) == 0) assert (not os.path.isfile("test.json")) c.load() assert (not c._data) assert (len(c) == 0) assert (not os.path.isfile("test.json"))
def test_load_default(tmp): c = ConfigurationFile("test.json", {"Default": True}) assert (len(c) == 1) assert (c["Default"] is True) assert (not os.path.isfile("test.json")) c.load() assert (len(c) == 1) assert (c["Default"] is True) assert (not os.path.isfile("test.json"))
def test_context_removes_file_readonly(tmp): assert (not os.path.isfile("test.json")) with ConfigurationFile("test.json", read_only=True): assert (os.path.isfile("test.json")) assert (not os.path.isfile("test.json"))
def test_context_doesnt_remove_default_file_non_readonly(tmp): assert (not os.path.isfile("test.json")) with ConfigurationFile("test.json", {"Default": True}): assert (os.path.isfile("test.json")) assert (os.path.isfile("test.json"))
def test_track_uses_config_file(tmp_json): cfg = ConfigurationFile(tmp_json, VariableTracking.config_schema_global) vtr = VariableTracking(cfg, None) vtr.ignore_global("FOO") assert (not vtr.is_tracked("FOO")) assert (not vtr.is_explicitly_configured_local("FOO")) assert (vtr.is_explicitly_configured_global("FOO")) cfg.save() cfg2 = ConfigurationFile(tmp_json, VariableTracking.config_schema_local) vtr2 = VariableTracking(None, cfg2) assert (not vtr2.is_tracked("FOO")) assert (vtr2.is_explicitly_configured_local("FOO")) assert (not vtr2.is_explicitly_configured_global("FOO"))
def test_context_loads(tmp): with open("test.json", 'w') as f: json.dump({"Default": False, "str": "Test string"}, f) with ConfigurationFile("test.json", {"Default": True}, read_only=True) \ as c: assert (c["Default"] is False) assert (c["str"] == "Test string")
def test_readonly(tmp): c = ConfigurationFile("test.json", {"Default": True}, read_only=True) assert (len(c) == 1) assert (c["Default"] is True) assert (not os.path.isfile("test.json")) with pytest.raises(PermissionError): c["other"] = "Foo" with pytest.raises(PermissionError): del c["Default"] assert (len(c) == 1) assert (list(c) == ["Default"]) with pytest.raises(io.UnsupportedOperation): c.save()
def test_track_uses_config_file(tmp_json): cfg = ConfigurationFile(tmp_json, VariableInformation.config_schema) info = VariableInformation(cfg) extended = EnvVarExtendedInformation() extended.description = "Test description" extended._type = "type1" info.set("FOO", extended, "test") cfg.save() cfg2 = ConfigurationFile(tmp_json, VariableInformation.config_schema) info2 = VariableInformation(cfg2) assert (info2["FOO"]["type"] == "type1") assert (info2["FOO"]["source"] == "test") assert (info2["FOO"]["description"] == "Test description") assert (info2["NEVER EXISTED"] is None)
def test_track_uses_config_file(tmp_json): cfg = ConfigurationFile(tmp_json, Snapshot.config_schema) snap = Snapshot(cfg) snap["FOO"] = {"XXX"} snap["BAR"] = ["Something"] snap["NUM"] = 8 del snap["UNDEFINE"] cfg.save() cfg2 = ConfigurationFile(tmp_json, Snapshot.config_schema) snap2 = Snapshot(cfg2) assert (snap2["FOO"] == {"XXX"}) assert (snap2["BAR"] == ["Something"]) assert (snap2["NUM"] == 8) assert (snap2["UNDEFINE"] is snap2.UNDEFINE) assert (snap2["UNDEFINE"] is not snap.UNDEFINE) # UNDEFINE tag is unique! assert (snap2["NEVER EXISTED"] is None)
def test_extended_encode_decode(tmp): a_list = [1, 2, 3, 'a', 'b', 'c', "Envprobe!"] a_set = set(a_list) a_tuple = tuple(a_list) c = ConfigurationFile("extended.json") c.load() c["mylist"] = a_list c["myset"] = a_set c["mytuple"] = a_tuple c.save() c = ConfigurationFile("extended.json") assert (not len(c)) c.load() assert (len(c) == 3) assert (c["mylist"] == a_list) assert (c["myset"] == a_set) assert (c["mytuple"] == a_tuple)
def test_nonlocal_file(tmp): c = ConfigurationFile(os.path.join("foo", "bar", "cfg.json")) c.load() c["x"] = 0 c.save() assert (os.path.isdir("foo")) assert (os.path.isdir(os.path.join("foo", "bar"))) assert (os.path.isfile(os.path.join("foo", "bar", "cfg.json")))
def test_context_saves(tmp): with open("test.json", 'w') as f: json.dump({"Default": False, "str": "Test string"}, f) with ConfigurationFile("test.json", { "Default": True, "New Default": 0 }) as c: assert (c["Default"] is False) assert (c["str"] == "Test string") assert (c["New Default"] == 0) c["str"] = "Foobar" with open("test.json", 'r') as f: data = json.load(f) assert (data == {"Default": False, "New Default": 0, "str": "Foobar"})
def test_delete_nondefault_value_then_save(tmp): with open("test.json", 'w') as f: json.dump({"Default": False, "str": "Test string"}, f) c = ConfigurationFile("test.json", {"Default": True}) c.load() del c["Default"] assert (len(c) == 1) assert (c["str"] == "Test string") c.save() with open("test.json", 'r') as f: data = json.load(f) assert (data == {"str": "Test string"})
def test_delete_default_value_then_save(tmp): c = ConfigurationFile("test.json", {"Default": True}) assert (not os.path.isfile("test.json")) c.load() assert (len(c) == 1) assert (c["Default"] is True) del c["Default"] assert (len(c) == 0) assert (c.changed) c.save() assert (not c.changed) with open("test.json", 'r') as f: data = json.load(f) assert (data == {})
def test_accessors(tmp): c = ConfigurationFile("test.json") assert (len(c) == 0) assert (not c.changed) c["x"] = "x" assert (c.changed) assert (len(c) == 1) assert (c["x"] == "x") assert (c.get("x") == "x") assert (c.get("x", 2) == "x") del c["x"] assert (not c.changed) # x was added and removed -> no change. assert (len(c) == 0) assert (c.get("x") is None) assert (c.get("x", 2) == 2) with pytest.raises(KeyError): print(c["x"])
def test_save_new_value(tmp): with open("test.json", 'w') as f: json.dump({"Default": False, "str": "Test string"}, f) c = ConfigurationFile("test.json", {"Default": True, "This is new": "yes"}) c.load() assert (len(c) == 3) assert (c["Default"] is False) assert (c["str"] == "Test string") assert (c["This is new"] == "yes") c.save() assert (not c.changed) with open("test.json", 'r') as f: data = json.load(f) assert (data == { "Default": False, "str": "Test string", "This is new": "yes" })
def test_save_empty(tmp): c = ConfigurationFile("test.json") c.save() assert (not os.path.isfile("test.json"))
def test_context_raises(tmp): with ConfigurationFile("test.json", read_only=True) as c: with pytest.raises(EnvironmentError): c.load() with pytest.raises(EnvironmentError): c.save()