class_name TestFiles extends RefCounted ## Temporary folders below user://tests for tests that write files. That ## folder lies inside the game's real user folder, next to the player's own ## key, save and recordings, so nothing outside it may be deleted here. ## The only folder the tests write to and remove_dir deletes from. const ROOT := "user://tests" static func temporary_dir(label: String) -> String: var path := ROOT.path_join("%s-%d" % [label, Time.get_ticks_usec()]) DirAccess.make_dir_recursive_absolute(path) return path ## Deletes the folder `path` with everything in it. Refuses every path that is ## not ROOT or below it, with an error and without deleting anything: a wrong ## argument must never delete the player's real files. static func remove_dir(path: String) -> void: var clean := path.simplify_path() if clean != ROOT and not clean.begins_with(ROOT + "/"): push_error("TestFiles.remove_dir refuses %s: it is not %s or below it" % [path, ROOT]) return for file in DirAccess.get_files_at(clean): DirAccess.remove_absolute(clean.path_join(file)) for directory in DirAccess.get_directories_at(clean): remove_dir(clean.path_join(directory)) DirAccess.remove_absolute(clean)