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
with open(os.path.join(self.wd, 'a.yaml'), 'w') as f: 'a.yaml': '---\n'
f.write('---\n'
'- 1 \n' '- 1 \n'
'- 2') '- 2',
# file with only one warning # file with only one warning
with open(os.path.join(self.wd, 'warn.yaml'), 'w') as f: 'warn.yaml': 'key: value\n',
f.write('key: value\n')
# .yml file at root # .yml file at root
open(os.path.join(self.wd, 'empty.yml'), 'w').close() 'empty.yml': '',
# file in dir # file in dir
os.mkdir(os.path.join(self.wd, 'sub')) 'sub/ok.yaml': '---\n'
with open(os.path.join(self.wd, 'sub', 'ok.yaml'), 'w') as f: 'key: value\n',
f.write('---\n'
'key: value\n')
# file in very nested dir # file in very nested dir
dir = self.wd 's/s/s/s/s/s/s/s/s/s/s/s/s/s/s/file.yaml': '---\n'
for i in range(15):
dir = os.path.join(dir, 's')
os.mkdir(dir)
with open(os.path.join(dir, 'file.yaml'), 'w') as f:
f.write('---\n'
'key: value\n' 'key: value\n'
'key: other value\n') 'key: other value\n',
# empty dir # empty dir
os.mkdir(os.path.join(self.wd, 'empty-dir')) 'empty-dir': [],
# non-YAML file # non-YAML file
with open(os.path.join(self.wd, 'no-yaml.json'), 'w') as f: 'no-yaml.json': '---\n'
f.write('---\n' 'key: value\n',
'key: value\n')
# non-ASCII chars # non-ASCII chars
os.mkdir(os.path.join(self.wd, 'non-ascii')) 'non-ascii/utf-8': (
with open(os.path.join(self.wd, 'non-ascii', 'utf-8'), 'wb') as f: u'---\n'
f.write((u'---\n'
u'- hétérogénéité\n' u'- hétérogénéité\n'
u'# 19.99 €\n' u'# 19.99 €\n'
u'- お早う御座います。\n' u'- お早う御座います。\n'
u'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8')) u'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8'),
})
def tearDown(self): def tearDown(self):
shutil.rmtree(self.wd) shutil.rmtree(self.wd)

Loading…
Cancel
Save