Remove DEFAULT_WORKBENCH_DIR.
[mediagoblin.git] / mediagoblin / tests / test_workbench.py
CommitLineData
2616d709 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
2616d709
CAW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import os
18import tempfile
19
2ecee34f 20
a32acafa 21from mediagoblin import workbench
9408938b
SS
22from mediagoblin.mg_globals import setup_globals
23from mediagoblin.decorators import get_workbench
f43ecb0f 24from mediagoblin.tests.test_storage import get_tmp_filestorage
2616d709
CAW
25
26
27class TestWorkbench(object):
28 def setUp(self):
29 self.workbench_manager = workbench.WorkbenchManager(
30 os.path.join(tempfile.gettempdir(), u'mgoblin_workbench_testing'))
31
32 def test_create_workbench(self):
bd6fe977 33 workbench = self.workbench_manager.create()
52426ae0
E
34 assert os.path.isdir(workbench.dir)
35 assert workbench.dir.startswith(self.workbench_manager.base_workbench_dir)
bd6fe977 36 workbench.destroy()
52426ae0
E
37
38 def test_joinpath(self):
bd6fe977 39 this_workbench = self.workbench_manager.create()
52426ae0
E
40 tmpname = this_workbench.joinpath('temp.txt')
41 assert tmpname == os.path.join(this_workbench.dir, 'temp.txt')
bd6fe977 42 this_workbench.destroy()
2616d709
CAW
43
44 def test_destroy_workbench(self):
45 # kill a workbench
bd6fe977 46 this_workbench = self.workbench_manager.create()
52426ae0
E
47 tmpfile_name = this_workbench.joinpath('temp.txt')
48 tmpfile = file(tmpfile_name, 'w')
2616d709
CAW
49 with tmpfile:
50 tmpfile.write('lollerskates')
51
52426ae0 52 assert os.path.exists(tmpfile_name)
2ecee34f 53
b67a983a 54 wb_dir = this_workbench.dir
bd6fe977 55 this_workbench.destroy()
52426ae0 56 assert not os.path.exists(tmpfile_name)
b67a983a 57 assert not os.path.exists(wb_dir)
f43ecb0f 58
68ffb136 59 def test_localized_file(self):
f43ecb0f 60 tmpdir, this_storage = get_tmp_filestorage()
bd6fe977 61 this_workbench = self.workbench_manager.create()
9408938b 62
f43ecb0f
CAW
63 # Write a brand new file
64 filepath = ['dir1', 'dir2', 'ourfile.txt']
65
66 with this_storage.get_file(filepath, 'w') as our_file:
67 our_file.write('Our file')
68
69 # with a local file storage
52426ae0 70 filename = this_workbench.localized_file(this_storage, filepath)
f43ecb0f
CAW
71 assert filename == os.path.join(
72 tmpdir, 'dir1/dir2/ourfile.txt')
73
74 # with a fake remote file storage
75 tmpdir, this_storage = get_tmp_filestorage(fake_remote=True)
76
77 # ... write a brand new file, again ;)
78 with this_storage.get_file(filepath, 'w') as our_file:
79 our_file.write('Our file')
80
8bfa533f 81 filename = this_workbench.localized_file(this_storage, filepath)
f43ecb0f 82 assert filename == os.path.join(
52426ae0 83 this_workbench.dir, 'ourfile.txt')
9408938b 84
f43ecb0f 85 # fake remote file storage, filename_if_copying set
8bfa533f
E
86 filename = this_workbench.localized_file(
87 this_storage, filepath, 'thisfile')
f43ecb0f 88 assert filename == os.path.join(
52426ae0 89 this_workbench.dir, 'thisfile.txt')
f43ecb0f
CAW
90
91 # fake remote file storage, filename_if_copying set,
92 # keep_extension_if_copying set to false
8bfa533f
E
93 filename = this_workbench.localized_file(
94 this_storage, filepath, 'thisfile.text', False)
f43ecb0f 95 assert filename == os.path.join(
52426ae0 96 this_workbench.dir, 'thisfile.text')
9408938b
SS
97
98 def test_workbench_decorator(self):
99 """Test @get_workbench decorator and automatic cleanup"""
100 # The decorator needs mg_globals.workbench_manager
101 setup_globals(workbench_manager=self.workbench_manager)
102
103 @get_workbench
104 def create_it(workbench=None):
105 # workbench dir exists?
106 assert os.path.isdir(workbench.dir)
107 return workbench.dir
108
109 benchdir = create_it()
110 # workbench dir has been cleaned up automatically?
111 assert not os.path.isdir(benchdir)