1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock
import fixtures
import io
import os
from reno import config
from reno import create
from reno.tests import base
class TestPickFileName(base.TestCase):
@mock.patch('os.path.exists')
def test_not_random_enough(self, exists):
exists.return_value = True
self.assertRaises(
ValueError,
create._pick_note_file_name,
'somepath',
'someslug',
)
@mock.patch('os.path.exists')
def test_random_enough(self, exists):
exists.return_value = False
result = create._pick_note_file_name('somepath', 'someslug')
self.assertIn('somepath', result)
self.assertIn('someslug', result)
class TestCreate(base.TestCase):
def setUp(self):
super(TestCreate, self).setUp()
self.tmpdir = self.useFixture(fixtures.TempDir()).path
def _create_user_template(self, contents):
filename = create._pick_note_file_name(self.tmpdir, 'usertemplate')
with open(filename, 'w') as f:
f.write(contents)
return filename
def _get_file_path_from_output(self, output):
# Get the last consecutive word from the output and remove the newline
return output[output.rfind(" ") + 1:-1]
def test_create_from_template(self):
filename = create._pick_note_file_name(self.tmpdir, 'theslug')
create._make_note_file(filename, 'i-am-a-template')
with open(filename, 'r') as f:
body = f.read()
self.assertEqual('i-am-a-template', body)
def test_create_from_user_template(self):
args = mock.Mock()
args.from_template = self._create_user_template('i-am-a-user-template')
args.slug = 'theslug'
args.edit = False
conf = mock.create_autospec(config.Config)
conf.notespath = self.tmpdir
conf.options = {'allow_subdirectories': False, 'encoding': None}
with mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
create.create_cmd(args, conf)
filename = self._get_file_path_from_output(fake_out.getvalue())
with open(filename, 'r') as f:
body = f.read()
self.assertEqual('i-am-a-user-template', body)
def test_create_from_user_template_fails_because_unexistent_file(self):
args = mock.Mock()
args.from_template = 'some-unexistent-file.yaml'
args.slug = 'theslug'
args.edit = False
conf = mock.create_autospec(config.Config)
conf.notespath = self.tmpdir
conf.options = {'allow_subdirectories': False, 'encoding': None}
self.assertRaises(ValueError, create.create_cmd, args, conf)
def test_create_from_user_template_fails_because_path_separator(self):
args = mock.Mock()
args.from_template = self._create_user_template('i-am-a-user-template')
args.slug = 'the' + os.sep + 'slug'
args.edit = False
conf = mock.create_autospec(config.Config)
conf.notespath = self.tmpdir
conf.options = {'allow_subdirectories': False, 'encoding': None}
self.assertRaises(ValueError, create.create_cmd, args, conf)
def test_create_from_template_with_path_separator_allowed(self):
args = mock.Mock()
args.from_template = self._create_user_template('i-am-a-user-template')
args.slug = 'the' + os.sep + 'slug'
args.edit = False
conf = mock.create_autospec(config.Config)
conf.notespath = self.tmpdir
conf.options = {'allow_subdirectories': True, 'encoding': None}
with mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
create.create_cmd(args, conf)
filename = self._get_file_path_from_output(fake_out.getvalue())
with open(filename, 'r') as f:
body = f.read()
self.assertEqual('i-am-a-user-template', body)
def test_edit(self):
self.useFixture(fixtures.EnvironmentVariable('EDITOR', 'myeditor'))
with mock.patch('subprocess.call') as call_mock:
self.assertTrue(create._edit_file('somepath'))
call_mock.assert_called_once_with(['myeditor', 'somepath'])
def test_edit_without_editor_env_var(self):
self.useFixture(fixtures.EnvironmentVariable('EDITOR'))
with mock.patch('subprocess.call') as call_mock:
self.assertFalse(create._edit_file('somepath'))
call_mock.assert_not_called()
|