Drop WorkbenchManager.localized_file()
[mediagoblin.git] / mediagoblin / tests / test_workbench.py
CommitLineData
2616d709
CAW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 Free Software Foundation, Inc
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
CAW
20from nose.tools import assert_raises
21
a32acafa 22from mediagoblin import workbench
f43ecb0f 23from mediagoblin.tests.test_storage import get_tmp_filestorage
2616d709
CAW
24
25
26class TestWorkbench(object):
27 def setUp(self):
28 self.workbench_manager = workbench.WorkbenchManager(
29 os.path.join(tempfile.gettempdir(), u'mgoblin_workbench_testing'))
30
31 def test_create_workbench(self):
32 workbench = self.workbench_manager.create_workbench()
52426ae0
E
33 assert os.path.isdir(workbench.dir)
34 assert workbench.dir.startswith(self.workbench_manager.base_workbench_dir)
35
36 def test_joinpath(self):
37 this_workbench = self.workbench_manager.create_workbench()
38 tmpname = this_workbench.joinpath('temp.txt')
39 assert tmpname == os.path.join(this_workbench.dir, 'temp.txt')
40 self.workbench_manager.destroy_workbench(this_workbench)
2616d709
CAW
41
42 def test_destroy_workbench(self):
43 # kill a workbench
2ecee34f 44 this_workbench = self.workbench_manager.create_workbench()
52426ae0
E
45 tmpfile_name = this_workbench.joinpath('temp.txt')
46 tmpfile = file(tmpfile_name, 'w')
2616d709
CAW
47 with tmpfile:
48 tmpfile.write('lollerskates')
49
52426ae0 50 assert os.path.exists(tmpfile_name)
2ecee34f
CAW
51
52 self.workbench_manager.destroy_workbench(this_workbench)
52426ae0
E
53 assert not os.path.exists(tmpfile_name)
54 assert not os.path.exists(this_workbench.dir)
2ecee34f
CAW
55
56 # make sure we can't kill other stuff though
52426ae0 57 dont_kill_this = workbench.Workbench(tempfile.mkdtemp())
2616d709 58
2ecee34f
CAW
59 assert_raises(
60 workbench.WorkbenchOutsideScope,
61 self.workbench_manager.destroy_workbench,
62 dont_kill_this)
f43ecb0f 63
68ffb136 64 def test_localized_file(self):
f43ecb0f
CAW
65 tmpdir, this_storage = get_tmp_filestorage()
66 this_workbench = self.workbench_manager.create_workbench()
67
68 # Write a brand new file
69 filepath = ['dir1', 'dir2', 'ourfile.txt']
70
71 with this_storage.get_file(filepath, 'w') as our_file:
72 our_file.write('Our file')
73
74 # with a local file storage
52426ae0 75 filename = this_workbench.localized_file(this_storage, filepath)
f43ecb0f
CAW
76 assert filename == os.path.join(
77 tmpdir, 'dir1/dir2/ourfile.txt')
78
79 # with a fake remote file storage
80 tmpdir, this_storage = get_tmp_filestorage(fake_remote=True)
81
82 # ... write a brand new file, again ;)
83 with this_storage.get_file(filepath, 'w') as our_file:
84 our_file.write('Our file')
85
8bfa533f 86 filename = this_workbench.localized_file(this_storage, filepath)
f43ecb0f 87 assert filename == os.path.join(
52426ae0 88 this_workbench.dir, 'ourfile.txt')
f43ecb0f
CAW
89
90 # fake remote file storage, filename_if_copying set
8bfa533f
E
91 filename = this_workbench.localized_file(
92 this_storage, filepath, 'thisfile')
f43ecb0f 93 assert filename == os.path.join(
52426ae0 94 this_workbench.dir, 'thisfile.txt')
f43ecb0f
CAW
95
96 # fake remote file storage, filename_if_copying set,
97 # keep_extension_if_copying set to false
8bfa533f
E
98 filename = this_workbench.localized_file(
99 this_storage, filepath, 'thisfile.text', False)
f43ecb0f 100 assert filename == os.path.join(
52426ae0 101 this_workbench.dir, 'thisfile.text')