Fixed the upgraderFileRename test

CURA-3479
This commit is contained in:
Jaime van Kessel 2017-05-01 17:28:55 +02:00
parent e6e442348b
commit c815077278

View file

@ -5,6 +5,7 @@ import os #To find the directory with test files and find the test files.
import pytest #This module contains unit tests.
import shutil #To copy files to make a temporary file.
import unittest.mock #To mock and monkeypatch stuff.
import urllib.parse
from cura.Settings.CuraContainerRegistry import CuraContainerRegistry #The class we're testing.
from cura.Settings.ExtruderStack import ExtruderStack #Testing for returning the correct types of stacks.
@ -59,20 +60,32 @@ def test_loadTypes(filename, output_class, container_registry):
## Tests whether loading a legacy file moves the upgraded file properly.
def test_loadLegacyFileRenamed(container_registry):
#Create a temporary file for the registry to load.
temp_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "temporary.stack.cfg")
temp_file_source = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks", "MachineLegacy.stack.cfg")
stacks_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "stacks")
temp_file = os.path.join(stacks_folder, "temporary.stack.cfg")
temp_file_source = os.path.join(stacks_folder, "MachineLegacy.stack.cfg")
shutil.copyfile(temp_file_source, temp_file)
#Mock some dependencies.
UM.Settings.ContainerStack.setContainerRegistry(container_registry)
Resources.getAllResourcesOfType = unittest.mock.MagicMock(return_value = [temp_file]) #Return a temporary file that we'll make for this test.
def findContainers(id, container_type = 0):
def findContainers(container_type = 0, id = None):
if id == "MachineLegacy":
return None
return [UM.Settings.ContainerRegistry._EmptyInstanceContainer(id)]
old_find_containers = container_registry.findContainers
container_registry.findContainers = findContainers
with unittest.mock.patch("cura.Settings.GlobalStack.GlobalStack.findContainer"):
container_registry.load()
container_registry.findContainers = old_find_containers
container_registry.saveAll()
print("all containers in registry", container_registry._containers)
assert not os.path.isfile(temp_file)
new_filename = os.path.splitext(os.path.splitext(temp_file)[0])[0] + ".global.cfg"
assert os.path.isfile(new_filename)
mime_type = container_registry.getMimeTypeForContainer(GlobalStack)
file_name = urllib.parse.quote_plus("MachineLegacy") + "." + mime_type.preferredSuffix
path = Resources.getStoragePath(Resources.ContainerStacks, file_name)
assert os.path.isfile(path)