tests(cli): Refactor temp test workspace recreation

Make it simpler and re-usable.
pull/60/head
Adrien Vergé 8 years ago
parent db116eaaaf
commit 7d638d47b9

@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import tempfile
import unittest import unittest
import yaml import yaml
@ -49,3 +51,21 @@ class RuleTestCase(unittest.TestCase):
real_problems = list(linter.run(source, self.build_fake_config(conf))) real_problems = list(linter.run(source, self.build_fake_config(conf)))
self.assertEqual(real_problems, expected_problems) self.assertEqual(real_problems, expected_problems)
def build_temp_workspace(files):
tempdir = tempfile.mkdtemp(prefix='yamllint-tests-')
for path, content in files.items():
path = os.path.join(tempdir, path)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
if type(content) is list:
os.mkdir(path)
else:
mode = 'wb' if isinstance(content, bytes) else 'w'
with open(path, mode) as f:
f.write(content)
return tempdir

@ -23,62 +23,45 @@ import locale
import os import os
import pty import pty
import shutil import shutil
import tempfile
import unittest import unittest
import sys import sys
from yamllint import cli from yamllint import cli
from tests.common import build_temp_workspace
class CommandLineTestCase(unittest.TestCase): class CommandLineTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.wd = tempfile.mkdtemp(prefix='yamllint-tests-') self.wd = build_temp_workspace({
# .yaml file at root
# .yaml file at root 'a.yaml': '---\n'
with open(os.path.join(self.wd, 'a.yaml'), 'w') as f: '- 1 \n'
f.write('---\n' '- 2',
'- 1 \n' # file with only one warning
'- 2') 'warn.yaml': 'key: value\n',
# .yml file at root
# file with only one warning 'empty.yml': '',
with open(os.path.join(self.wd, 'warn.yaml'), 'w') as f: # file in dir
f.write('key: value\n') 'sub/ok.yaml': '---\n'
'key: value\n',
# .yml file at root # file in very nested dir
open(os.path.join(self.wd, 'empty.yml'), 'w').close() 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml': '---\n'
'key: value\n'
# file in dir 'key: other value\n',
os.mkdir(os.path.join(self.wd, 'sub')) # empty dir
with open(os.path.join(self.wd, 'sub', 'ok.yaml'), 'w') as f: 'empty-dir': [],
f.write('---\n' # non-YAML file
'key: value\n') 'no-yaml.json': '---\n'
'key: value\n',
# file in very nested dir # non-ASCII chars
dir = self.wd 'non-ascii/utf-8': (
for i in range(15): u'---\n'
dir = os.path.join(dir, 's') u'- hétérogénéité\n'
os.mkdir(dir) u'# 19.99 €\n'
with open(os.path.join(dir, 'file.yaml'), 'w') as f: u'- お早う御座います。\n'
f.write('---\n' u'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8'),
'key: value\n' })
'key: other value\n')
# empty dir
os.mkdir(os.path.join(self.wd, 'empty-dir'))
# non-YAML file
with open(os.path.join(self.wd, 'no-yaml.json'), 'w') as f:
f.write('---\n'
'key: value\n')
# non-ASCII chars
os.mkdir(os.path.join(self.wd, 'non-ascii'))
with open(os.path.join(self.wd, 'non-ascii', 'utf-8'), 'wb') as f:
f.write((u'---\n'
u'- hétérogénéité\n'
u'# 19.99 €\n'
u'- お早う御座います。\n'
u'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8'))
def tearDown(self): def tearDown(self):
shutil.rmtree(self.wd) shutil.rmtree(self.wd)

Loading…
Cancel
Save