636c86896e8dab53590d60d43660e3599fe43830
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
21 from mediagoblin
.tools
import workbench
22 from mediagoblin
.mg_globals
import setup_globals
23 from mediagoblin
.decorators
import get_workbench
24 from mediagoblin
.tests
.test_storage
import get_tmp_filestorage
27 class TestWorkbench(object):
29 self
.workbench_manager
= workbench
.WorkbenchManager(
30 os
.path
.join(tempfile
.gettempdir(), u
'mgoblin_workbench_testing'))
32 def test_create_workbench(self
):
33 workbench
= self
.workbench_manager
.create()
34 assert os
.path
.isdir(workbench
.dir)
35 assert workbench
.dir.startswith(self
.workbench_manager
.base_workbench_dir
)
38 def test_joinpath(self
):
39 this_workbench
= self
.workbench_manager
.create()
40 tmpname
= this_workbench
.joinpath('temp.txt')
41 assert tmpname
== os
.path
.join(this_workbench
.dir, 'temp.txt')
42 this_workbench
.destroy()
44 def test_destroy_workbench(self
):
46 this_workbench
= self
.workbench_manager
.create()
47 tmpfile_name
= this_workbench
.joinpath('temp.txt')
48 tmpfile
= file(tmpfile_name
, 'w')
50 tmpfile
.write('lollerskates')
52 assert os
.path
.exists(tmpfile_name
)
54 wb_dir
= this_workbench
.dir
55 this_workbench
.destroy()
56 assert not os
.path
.exists(tmpfile_name
)
57 assert not os
.path
.exists(wb_dir
)
59 def test_localized_file(self
):
60 tmpdir
, this_storage
= get_tmp_filestorage()
61 this_workbench
= self
.workbench_manager
.create()
63 # Write a brand new file
64 filepath
= ['dir1', 'dir2', 'ourfile.txt']
66 with this_storage
.get_file(filepath
, 'w') as our_file
:
67 our_file
.write('Our file')
69 # with a local file storage
70 filename
= this_workbench
.localized_file(this_storage
, filepath
)
71 assert filename
== os
.path
.join(
72 tmpdir
, 'dir1/dir2/ourfile.txt')
74 # with a fake remote file storage
75 tmpdir
, this_storage
= get_tmp_filestorage(fake_remote
=True)
77 # ... write a brand new file, again ;)
78 with this_storage
.get_file(filepath
, 'w') as our_file
:
79 our_file
.write('Our file')
81 filename
= this_workbench
.localized_file(this_storage
, filepath
)
82 assert filename
== os
.path
.join(
83 this_workbench
.dir, 'ourfile.txt')
85 # fake remote file storage, filename_if_copying set
86 filename
= this_workbench
.localized_file(
87 this_storage
, filepath
, 'thisfile')
88 assert filename
== os
.path
.join(
89 this_workbench
.dir, 'thisfile.txt')
91 # fake remote file storage, filename_if_copying set,
92 # keep_extension_if_copying set to false
93 filename
= this_workbench
.localized_file(
94 this_storage
, filepath
, 'thisfile.text', False)
95 assert filename
== os
.path
.join(
96 this_workbench
.dir, 'thisfile.text')
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
)
104 def create_it(workbench
=None):
105 # workbench dir exists?
106 assert os
.path
.isdir(workbench
.dir)
109 benchdir
= create_it()
110 # workbench dir has been cleaned up automatically?
111 assert not os
.path
.isdir(benchdir
)