| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
import fixtures |
| 18 |
import io |
| 19 |
import os |
| 20 |
|
| 21 |
from reno import config |
| 22 |
from reno import create |
| 23 |
from reno.tests import base |
| 24 |
|
| 25 |
|
| 26 |
class TestPickFileName(base.TestCase): |
| 27 |
|
| 28 |
@mock.patch('os.path.exists') |
| 29 |
def test_not_random_enough(self, exists): |
| 30 |
exists.return_value = True |
| 31 |
self.assertRaises( |
| 32 |
ValueError, |
| 33 |
create._pick_note_file_name, |
| 34 |
'somepath', |
| 35 |
'someslug', |
| 36 |
) |
| 37 |
|
| 38 |
@mock.patch('os.path.exists') |
| 39 |
def test_random_enough(self, exists): |
| 40 |
exists.return_value = False |
| 41 |
result = create._pick_note_file_name('somepath', 'someslug') |
| 42 |
self.assertIn('somepath', result) |
| 43 |
self.assertIn('someslug', result) |
| 44 |
|
| 45 |
|
| 46 |
class TestCreate(base.TestCase): |
| 47 |
|
| 48 |
def setUp(self): |
| 49 |
super(TestCreate, self).setUp() |
| 50 |
self.tmpdir = self.useFixture(fixtures.TempDir()).path |
| 51 |
|
| 52 |
def _create_user_template(self, contents): |
| 53 |
filename = create._pick_note_file_name(self.tmpdir, 'usertemplate') |
| 54 |
with open(filename, 'w') as f: |
| 55 |
f.write(contents) |
| 56 |
return filename |
| 57 |
|
| 58 |
def _get_file_path_from_output(self, output): |
| 59 |
|
| 60 |
return output[output.rfind(" ") + 1:-1] |
| 61 |
|
| 62 |
def test_create_from_template(self): |
| 63 |
filename = create._pick_note_file_name(self.tmpdir, 'theslug') |
| 64 |
create._make_note_file(filename, 'i-am-a-template') |
| 65 |
with open(filename, 'r') as f: |
| 66 |
body = f.read() |
| 67 |
self.assertEqual('i-am-a-template', body) |
| 68 |
|
| 69 |
def test_create_from_user_template(self): |
| 70 |
args = mock.Mock() |
| 71 |
args.from_template = self._create_user_template('i-am-a-user-template') |
| 72 |
args.slug = 'theslug' |
| 73 |
args.edit = False |
| 74 |
conf = mock.create_autospec(config.Config) |
| 75 |
conf.notespath = self.tmpdir |
| 76 |
conf.options = {'allow_subdirectories': False, 'encoding': None} |
| 77 |
with mock.patch('sys.stdout', new=io.StringIO()) as fake_out: |
| 78 |
create.create_cmd(args, conf) |
| 79 |
filename = self._get_file_path_from_output(fake_out.getvalue()) |
| 80 |
with open(filename, 'r') as f: |
| 81 |
body = f.read() |
| 82 |
self.assertEqual('i-am-a-user-template', body) |
| 83 |
|
| 84 |
def test_create_from_user_template_fails_because_unexistent_file(self): |
| 85 |
args = mock.Mock() |
| 86 |
args.from_template = 'some-unexistent-file.yaml' |
| 87 |
args.slug = 'theslug' |
| 88 |
args.edit = False |
| 89 |
conf = mock.create_autospec(config.Config) |
| 90 |
conf.notespath = self.tmpdir |
| 91 |
conf.options = {'allow_subdirectories': False, 'encoding': None} |
| 92 |
self.assertRaises(ValueError, create.create_cmd, args, conf) |
| 93 |
|
| 94 |
def test_create_from_user_template_fails_because_path_separator(self): |
| 95 |
args = mock.Mock() |
| 96 |
args.from_template = self._create_user_template('i-am-a-user-template') |
| 97 |
args.slug = 'the' + os.sep + 'slug' |
| 98 |
args.edit = False |
| 99 |
conf = mock.create_autospec(config.Config) |
| 100 |
conf.notespath = self.tmpdir |
| 101 |
conf.options = {'allow_subdirectories': False, 'encoding': None} |
| 102 |
self.assertRaises(ValueError, create.create_cmd, args, conf) |
| 103 |
|
| 104 |
def test_create_from_template_with_path_separator_allowed(self): |
| 105 |
args = mock.Mock() |
| 106 |
args.from_template = self._create_user_template('i-am-a-user-template') |
| 107 |
args.slug = 'the' + os.sep + 'slug' |
| 108 |
args.edit = False |
| 109 |
conf = mock.create_autospec(config.Config) |
| 110 |
conf.notespath = self.tmpdir |
| 111 |
conf.options = {'allow_subdirectories': True, 'encoding': None} |
| 112 |
with mock.patch('sys.stdout', new=io.StringIO()) as fake_out: |
| 113 |
create.create_cmd(args, conf) |
| 114 |
filename = self._get_file_path_from_output(fake_out.getvalue()) |
| 115 |
with open(filename, 'r') as f: |
| 116 |
body = f.read() |
| 117 |
self.assertEqual('i-am-a-user-template', body) |
| 118 |
|
| 119 |
def test_edit(self): |
| 120 |
self.useFixture(fixtures.EnvironmentVariable('EDITOR', 'myeditor')) |
| 121 |
with mock.patch('subprocess.call') as call_mock: |
| 122 |
self.assertTrue(create._edit_file('somepath')) |
| 123 |
call_mock.assert_called_once_with(['myeditor', 'somepath']) |
| 124 |
|
| 125 |
def test_edit_without_editor_env_var(self): |
| 126 |
self.useFixture(fixtures.EnvironmentVariable('EDITOR')) |
| 127 |
with mock.patch('subprocess.call') as call_mock: |
| 128 |
self.assertFalse(create._edit_file('somepath')) |
| 129 |
call_mock.assert_not_called() |