From 2168860a1ed9bb6744622d9d097c347e0193fee0 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sun, 4 Dec 2016 01:06:39 +0200 Subject: [PATCH] Added unit test for rewriter. --- run_unittests.py | 42 ++++++++++++++++++++++++++ test cases/rewrite/1 basic/added.txt | 5 +++ test cases/rewrite/1 basic/meson.build | 5 +++ test cases/rewrite/1 basic/removed.txt | 5 +++ 4 files changed, 57 insertions(+) create mode 100644 test cases/rewrite/1 basic/added.txt create mode 100644 test cases/rewrite/1 basic/meson.build create mode 100644 test cases/rewrite/1 basic/removed.txt diff --git a/run_unittests.py b/run_unittests.py index fff0c359d..756579cf3 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -17,6 +17,7 @@ import unittest, os, sys, shutil, time import subprocess import re, json import tempfile +from glob import glob import mesonbuild.environment from mesonbuild.environment import detect_ninja from mesonbuild.dependencies import PkgConfigDependency, Qt5Dependency @@ -203,5 +204,46 @@ class LinuxlikeTests(unittest.TestCase): mesonlog = self.get_meson_log() self.assertTrue(msg in mesonlog) +class RewriterTests(unittest.TestCase): + + def setUp(self): + super().setUp() + src_root = os.path.dirname(__file__) + self.testroot = tempfile.mkdtemp() + self.rewrite_command = [sys.executable, os.path.join(src_root, 'mesonast.py')] + self.builddir = tempfile.mkdtemp() + self.test_dir = os.path.join(src_root, 'test cases/rewrite') + + def tearDown(self): + shutil.rmtree(self.builddir) + + def check_effectively_same(self, mainfile, truth): + with open(os.path.join(self.builddir, mainfile)) as f: + mf = f.read() + with open(os.path.join(self.builddir, truth)) as f: + t = f.read() + # Rewriting is not guaranteed to do a perfect job of + # maintaining whitespace. + self.assertEqual(mf.replace(' ', ''), t.replace(' ', '')) + + def test_basic(self): + for f in glob(os.path.join(self.test_dir, '1 basic/*')): + shutil.copy(f, self.builddir) + subprocess.check_call(self.rewrite_command + ['remove', + '--target=trivialprog', + '--filename=notthere.c', + '--sourcedir', self.builddir]) + self.check_effectively_same('meson.build', 'removed.txt') + subprocess.check_call(self.rewrite_command + ['add', + '--target=trivialprog', + '--filename=notthere.c', + '--sourcedir', self.builddir]) + self.check_effectively_same('meson.build', 'added.txt') + subprocess.check_call(self.rewrite_command + ['remove', + '--target=trivialprog', + '--filename=notthere.c', + '--sourcedir', self.builddir]) + self.check_effectively_same('meson.build', 'removed.txt') + if __name__ == '__main__': unittest.main() diff --git a/test cases/rewrite/1 basic/added.txt b/test cases/rewrite/1 basic/added.txt new file mode 100644 index 000000000..657dd42fb --- /dev/null +++ b/test cases/rewrite/1 basic/added.txt @@ -0,0 +1,5 @@ +project('rewritetest', 'c') + +sources = ['trivial.c'] + +exe = executable('trivialprog', 'notthere.c', sources) diff --git a/test cases/rewrite/1 basic/meson.build b/test cases/rewrite/1 basic/meson.build new file mode 100644 index 000000000..a0485d079 --- /dev/null +++ b/test cases/rewrite/1 basic/meson.build @@ -0,0 +1,5 @@ +project('rewritetest', 'c') + +sources = ['trivial.c', 'notthere.c'] + +exe = executable('trivialprog', sources) diff --git a/test cases/rewrite/1 basic/removed.txt b/test cases/rewrite/1 basic/removed.txt new file mode 100644 index 000000000..55192149c --- /dev/null +++ b/test cases/rewrite/1 basic/removed.txt @@ -0,0 +1,5 @@ +project('rewritetest', 'c') + +sources = ['trivial.c'] + +exe = executable('trivialprog', sources)