File: test_main.py

package info (click to toggle)
jhbuild 3.38.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,640 kB
  • sloc: python: 11,621; sh: 4,548; makefile: 204; ansic: 19; sed: 16
file content (768 lines) | stat: -rw-r--r-- 33,381 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#! /usr/bin/env python2
# jhbuild - a tool to ease building collections of source packages
# Copyright (C) 2001-2006  James Henstridge
# Copyright (C) 2007-2008  Frederic Peters
#
#   tests.py: unit tests
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from __future__ import print_function

import os
import shutil
import logging
import subprocess
import sys
import glob
import tempfile
import unittest

from jhbuild.utils.compat import builtins, iteritems

SRCDIR = os.path.join(os.path.dirname(__file__), '..')

builtins.__dict__['PKGDATADIR'] = None
builtins.__dict__['DATADIR'] = None
builtins.__dict__['SRCDIR'] = SRCDIR

sys.path.insert(0, SRCDIR)

# Override jhbuild.utils.systeminstall with this module 'tests'
import jhbuild.utils.systeminstall
sys.modules['jhbuild.utils.systeminstall'] = sys.modules[__name__]
sys.modules['jhbuild.utils'].systeminstall = sys.modules[__name__]

from jhbuild.errors import UsageError, CommandError
from jhbuild.modtypes import Package
from jhbuild.modtypes.autotools import AutogenModule
from jhbuild.modtypes.distutils import DistutilsModule
import jhbuild.config
import jhbuild.frontends.terminal
import jhbuild.moduleset
import jhbuild.utils.cmds
import jhbuild.versioncontrol.tarball
from jhbuild.utils.sxml import sxml_to_string
from jhbuild.utils.cmds import pprint_output

from . import mock

if sys.platform.startswith('win'):
    import jhbuild.utils.subprocess_win32 as subprocess_win32
    class WindowsTestCase(unittest.TestCase):
        '''Tests for Windows kludges.'''
        def testCmdline2List(self):
            cmdline = 'test "no quotes" != \\"no\\ quotes\\"'
            cmd_list = subprocess_win32.cmdline2list (cmdline)
            self.assertEqual (cmd_list, ['test', 'no quotes', '!=', '"no\\ quotes"'])


def has_xmllint():
    try:
        subprocess.check_output(['xmllint'], stderr=subprocess.STDOUT)
    except OSError:
        return False
    except subprocess.CalledProcessError:
        pass
    return True


def has_trang():
    try:
        subprocess.check_output(['trang'], stderr=subprocess.STDOUT)
    except OSError:
        return False
    except subprocess.CalledProcessError:
        pass
    return True


class ModulesetXMLTest(unittest.TestCase):
    """Check the modulesets for validity.

    This is a lower-level check than `jhbuild checkmodulesets` (which analyses the
    module graph), and doesn't require jhbuild to be built and installed.
    """

    @unittest.skipUnless(has_xmllint(), "no xmllint")
    def test_dtd(self):
        modulesets = os.path.join(SRCDIR, 'modulesets')
        modules = glob.glob(os.path.join(modulesets, '*.modules'))
        dtd = os.path.join(modulesets, 'moduleset.dtd')
        try:
            subprocess.check_output(
                ['xmllint', '--noout', '--dtdvalid', dtd] + modules, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            raise Exception(e.output)

    @unittest.skipUnless(has_xmllint(), "no xmllint")
    @unittest.skipUnless(has_trang(), "no trang")
    def test_relaxng(self):
        modulesets = os.path.join(SRCDIR, 'modulesets')
        modules = glob.glob(os.path.join(modulesets, '*.modules'))
        rnc = os.path.join(modulesets, 'moduleset.rnc')
        temp_dir = tempfile.mkdtemp()
        rng = os.path.join(temp_dir, 'moduleset.rng')
        try:
            subprocess.check_output(
                ['trang', rnc, rng], stderr=subprocess.STDOUT)
            subprocess.check_output(
                ['xmllint', '--noout', '--relaxng', rng] + modules, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            raise Exception(e.output)
        finally:
            shutil.rmtree(temp_dir)


class CmdTestCase(unittest.TestCase):

    def test_pprint_output(self):
        try:
            p = subprocess.Popen(
                ["echo", "foo\nbar"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        except OSError:
            raise unittest.SkipTest("no echo command")
        arguments = []
        pprint_output(p, lambda *args: arguments.append(args))
        self.assertEqual(arguments, [('foo\n', False), ('bar\n', False)])


class _TestConfig(jhbuild.config.Config):

    # The Config base class calls setup_env() in the constructor, but
    # we need to override some attributes before calling it.
    def setup_env(self):
        pass

    def real_setup_env(self):
        from jhbuild.environment import setup_env
        setup_env(self.prefix)

class JhbuildConfigTestCase(unittest.TestCase):
    """A test case that creates a mock configuration and temporary directory."""

    def setUp(self):
        self.config = mock.Config()
        self._old_env = os.environ.copy()
        self._temp_dirs = []

    def tearDown(self):
        restore_environ(self._old_env)
        for temp_dir in self._temp_dirs:
            shutil.rmtree(temp_dir)

    def make_temp_dir(self):
        temp_dir = tempfile.mkdtemp(prefix='unittest-')
        self._temp_dirs.append(temp_dir)
        return temp_dir

    def make_config(self):
        temp_dir = self.make_temp_dir()
        config = _TestConfig(None, [])
        config.checkoutroot = os.path.abspath(os.path.join(temp_dir, 'checkout'))
        config.prefix = os.path.abspath(os.path.join(temp_dir, 'prefix'))
        config.top_builddir = os.path.join(config.prefix, '_jhbuild')
        os.makedirs(config.checkoutroot)
        os.makedirs(config.prefix)
        config.buildroot = None
        config.interact = False
        config.use_local_modulesets = True
        config.quiet_mode = True # Not enough to disable output entirely
        config.progress_bar = False
        config.real_setup_env()
        return config

    def make_branch(self, config, src_name):
        branch_dir = os.path.join(config.checkoutroot, src_name)
        shutil.copytree(os.path.join(os.path.dirname(__file__), src_name),
                        branch_dir)
        # With 'make distcheck' the source is read only, so we need to chmod
        # after copying
        os.chmod(branch_dir, 0o777)
        return SimpleBranch(src_name, branch_dir)

    def make_terminal_buildscript(self, config, module_list):
        module_set = jhbuild.moduleset.load(config)
        module_set.packagedb = mock.PackageDB()
        return jhbuild.frontends.terminal.TerminalBuildScript(config, module_list, module_set)


class CMakeModuleTest(unittest.TestCase):

    def test_to_sxml(self):
        from jhbuild.modtypes.cmake import CMakeModule

        mod = CMakeModule("foo")
        self.assertEqual(
            sxml_to_string(mod.to_sxml()),
            '<cmake id="foo"><dependencies></dependencies>\n<branch></branch></cmake>')


class ModuleOrderingTestCase(JhbuildConfigTestCase):
    '''Module Ordering'''

    def setUp(self):
        super(ModuleOrderingTestCase, self).setUp()
        self.moduleset = jhbuild.moduleset.ModuleSet(config=self.config)
        self.moduleset.add(Package('foo'))
        self.moduleset.add(Package('bar'))
        self.moduleset.add(Package('baz'))
        self.moduleset.add(Package('qux'))
        self.moduleset.add(Package('quux'))
        self.moduleset.add(Package('corge'))

    def get_module_list(self, seed, skip=[], tags=[], include_suggests=True,
                        include_afters=False):
        return [x.name for x in self.moduleset.get_module_list(
                    seed, skip, tags, include_suggests,
                    include_afters)]

    def test_standalone_one(self):
        '''A standalone module'''
        self.assertEqual(self.get_module_list(['foo']), ['foo'])

    def test_standalone_two(self):
        '''Two standalone modules'''
        self.assertEqual(self.get_module_list(['foo', 'bar']), ['foo', 'bar'])

    def test_dependency_chain_straight(self):
        '''A straight chain of dependencies'''
        self.moduleset.modules['foo'].dependencies = ['bar']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])

    def test_dependency_chain_straight_skip(self):
        '''A straight chain of dependencies, with a module to skip'''
        self.moduleset.modules['foo'].dependencies = ['bar']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.assertEqual(self.get_module_list(['foo'], ['bar']), ['foo'])

    def test_dependency_chain_bi(self):
        '''A dividing chain of dependencies'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'qux']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.modules['qux'].dependencies = ['quux']
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'quux', 'qux', 'foo'])

    def test_dependency_cycle(self):
        '''A chain of dependencies with a cycle'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'qux']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.modules['qux'].dependencies = ['quux', 'foo']
        self.moduleset.raise_exception_on_warning = True
        self.assertRaises(UsageError, self.get_module_list, ['foo'])
        self.moduleset.raise_exception_on_warning = False

    def test_dependency_chain_missing_dependencies(self):
        '''A chain of dependencies with a missing <dependencies> module'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'plop']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.raise_exception_on_warning = True
        self.assertRaises(UsageError, self.get_module_list, ['foo'])
        self.moduleset.raise_exception_on_warning = False
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])

    def test_dependency_chain_missing_after(self):
        '''A chain of dependencies with a missing <after> module'''
        self.moduleset.modules['foo'].dependencies = ['bar']
        self.moduleset.modules['foo'].after = ['plop']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])

    def test_dependency_chain_missing_suggests(self):
        '''A chain of dependencies with a missing <suggests> module'''
        self.moduleset.modules['foo'].dependencies = ['bar']
        self.moduleset.modules['foo'].suggests = ['plop']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])

    def test_dependency_chain_after(self):
        '''A dividing chain of dependencies with an <after> module'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'qux']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.modules['baz'].after = ['qux']
        self.moduleset.modules['qux'].dependencies = ['quux']
        self.assertEqual(self.get_module_list(['foo'], include_afters=True), ['quux', 'qux', 'baz', 'bar', 'foo'])

    def test_dependency_chain_suggests(self):
        '''A dividing chain of dependencies with an <suggests> module'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'qux']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.modules['baz'].suggests = ['qux']
        self.moduleset.modules['qux'].dependencies = ['quux']
        self.assertEqual(self.get_module_list(['foo']), ['quux', 'qux', 'baz', 'bar', 'foo'])

    def test_dependency_cycle_after(self):
        '''A chain of dependencies with a cycle caused by an <after> module'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'qux']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.modules['qux'].dependencies = ['quux']
        self.moduleset.modules['qux'].after = ['foo']
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'quux', 'qux', 'foo'])

    def test_dependency_cycle_suggests(self):
        '''A chain of dependencies with a cycle caused by an <suggests> module'''
        self.moduleset.modules['foo'].dependencies = ['bar', 'qux']
        self.moduleset.modules['bar'].dependencies = ['baz']
        self.moduleset.modules['qux'].dependencies = ['quux']
        self.moduleset.modules['qux'].suggests = ['foo']
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'quux', 'qux', 'foo'])

    def test_dependency_chain_recursive_after(self):
        '''A chain of dependencies with a recursively defined <after> module'''
        # see http://bugzilla.gnome.org/show_bug.cgi?id=546640
        self.moduleset.modules['foo'] # gtk-doc
        self.moduleset.modules['bar'].dependencies = ['foo'] # meta-bootstrap
        self.moduleset.modules['bar'].type = 'meta'
        self.moduleset.modules['baz'].after = ['bar'] # cairo
        self.moduleset.modules['qux'].dependencies = ['baz'] # meta-stuff
        self.assertEqual(self.get_module_list(['qux', 'foo']), ['foo', 'baz', 'qux'])

    def test_dependency_chain_recursive_after_dependencies(self):
        '''A chain dependency with an <after> module depending on an inversed relation'''
        # see http://bugzilla.gnome.org/show_bug.cgi?id=546640
        self.moduleset.modules['foo'] # nautilus
        self.moduleset.modules['bar'] # nautilus-cd-burner
        self.moduleset.modules['baz'] # tracker
        self.moduleset.modules['foo'].after = ['baz']
        self.moduleset.modules['bar'].dependencies = ['foo']
        self.moduleset.modules['baz'].dependencies = ['bar']
        self.moduleset.raise_exception_on_warning = True
        self.assertRaises(UsageError, self.get_module_list, ['foo', 'bar'])
        self.moduleset.raise_exception_on_warning = False

    def test_sys_deps(self):
        '''deps ommitted because satisfied by system dependencies'''
        class TestBranch(jhbuild.versioncontrol.tarball.TarballBranch):
            version = None

            def __init__(self):
                pass

        self.moduleset.add(Package('syspkgalpha'))
        self.moduleset.modules['foo'].dependencies = ['bar']
        self.moduleset.modules['bar'].dependencies = ['syspkgalpha']
        self.moduleset.modules['syspkgalpha'].dependencies = ['baz']
        self.moduleset.modules['syspkgalpha'].pkg_config = 'syspkgalpha.pc'
        self.moduleset.modules['syspkgalpha'].branch = TestBranch()
        self.moduleset.modules['syspkgalpha'].branch.version = '3'
        self.assertEqual(self.get_module_list(['foo']),
                         ['baz', 'syspkgalpha', 'bar', 'foo'])
        self.moduleset.modules['syspkgalpha'].branch.version = '3.1'
        self.assertEqual(self.get_module_list(['foo']),
                         ['baz', 'syspkgalpha', 'bar', 'foo'])
        self.moduleset.modules['syspkgalpha'].branch.version = '2'
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])
        self.moduleset.modules['syspkgalpha'].branch.version = '1'
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])
        self.moduleset.modules['syspkgalpha'].branch.version = '1.1'
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])

        self.moduleset.add(Package('syspkgbravo'))
        self.moduleset.modules['foo'].dependencies = ['bar']
        self.moduleset.modules['bar'].dependencies = ['syspkgbravo']
        self.moduleset.modules['syspkgbravo'].dependencies = ['baz']
        self.moduleset.modules['syspkgbravo'].pkg_config = 'syspkgbravo.pc'
        self.moduleset.modules['syspkgbravo'].branch = TestBranch()
        self.moduleset.modules['syspkgbravo'].branch.version = '3'
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])
        self.moduleset.modules['syspkgbravo'].branch.version = '3.3'
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])
        self.moduleset.modules['syspkgbravo'].branch.version = '3.4'
        self.assertEqual(self.get_module_list(['foo']), ['baz', 'bar', 'foo'])
        self.moduleset.modules['syspkgbravo'].branch.version = '3.5'
        self.assertEqual(self.get_module_list(['foo']),
                         ['baz', 'syspkgbravo', 'bar', 'foo'])
        self.moduleset.modules['syspkgbravo'].branch.version = '4'
        self.assertEqual(self.get_module_list(['foo']),
                         ['baz', 'syspkgbravo', 'bar', 'foo'])


class BuildTestCase(JhbuildConfigTestCase):
    def setUp(self):
        super(BuildTestCase, self).setUp()
        self.branch = mock.Branch(os.path.join(self.config.buildroot, 'nonexistent'))
        self.branch.config = self.config
        self.packagedb = None
        self.buildscript = None
        self.moduleset = None
        os.environ['JHBUILD_PREFIX'] = self.config.prefix

    def tearDown(self):
        super(BuildTestCase, self).tearDown()
        self.buildscript = None

    def build(self, packagedb_params = {}, **kwargs):
        self.config.build_targets = ['install', 'test']
        for k in kwargs:
            setattr(self.config, k, kwargs[k])
        self.config.update_build_targets()

        if (self.packagedb is None) or (len(packagedb_params) > 0):
            self.packagedb = mock.PackageDB(**packagedb_params)
            self.moduleset = jhbuild.moduleset.ModuleSet(self.config, db=self.packagedb)
        self.buildscript = mock.BuildScript(self.config, self.modules, self.moduleset)

        self.buildscript.build()
        return self.buildscript.actions

class AutotoolsModTypeTestCase(BuildTestCase):
    '''Autotools steps'''

    def setUp(self):
        super(AutotoolsModTypeTestCase, self).setUp()
        module = mock.MockModule('foo', branch=self.branch)
        self.modules = [module]
        self.modules[0].config = self.config
        # replace clean method as it checks for Makefile existence
        self.modules[0].skip_clean = lambda x,y: False

    def test_build(self):
        '''Building a autotools module'''
        self.assertEqual(self.build(),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building',
                 'foo:Installing'])

    def test_build_no_network(self):
        '''Building a autotools module, without network'''
        self.assertEqual(self.build(nonetwork = True),
                ['foo:Configuring', 'foo:Building', 'foo:Installing'])

    def test_update(self):
        '''Updating a autotools module'''
        self.assertEqual(self.build(nobuild = True), ['foo:Checking out'])

    def test_build_check(self):
        '''Building a autotools module, with checks'''
        self.assertEqual(self.build(makecheck = True),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building',
                 'foo:Checking', 'foo:Installing'])

    def test_build_clean_and_check(self):
        '''Building a autotools module, with cleaning and checks'''
        self.assertEqual(self.build(makecheck = True, makeclean = True),
                ['foo:Checking out', 'foo:Configuring', 'foo:Cleaning',
                 'foo:Building', 'foo:Checking', 'foo:Installing'])

    def test_build_check_error(self):
        '''Building a autotools module, with an error in make check'''

        def make_check_error(buildscript, *args):
            self.modules[0].do_check_orig(buildscript, *args)
            raise CommandError('Mock Command Error Exception')
        make_check_error.depends = self.modules[0].do_check.depends
        make_check_error.error_phases = self.modules[0].do_check.error_phases
        self.modules[0].do_check_orig = self.modules[0].do_check
        self.modules[0].do_check = make_check_error

        self.assertEqual(self.build(makecheck = True),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building',
                 'foo:Checking [error]'])


class BuildPolicyTestCase(BuildTestCase):
    '''Build Policy'''

    def setUp(self):
        super(BuildPolicyTestCase, self).setUp()
        self.modules = [mock.MockModule('foo', branch=self.branch)]
        self.modules[0].config = self.config

    def test_policy_all(self):
        '''Building an uptodate module with build policy set to "all"'''
        self.config.build_policy = 'all'
        self.assertEqual(self.build(packagedb_params = {'uptodate': True}),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building',
                 'foo:Installing'])

    def test_policy_updated(self):
        '''Building an uptodate module with build policy set to "updated"'''
        self.config.build_policy = 'updated'
        self.assertEqual(self.build(packagedb_params = {'uptodate': True}),
                ['foo:Checking out'])

    def test_policy_all_with_no_network(self):
        '''Building an uptodate module with "all" policy, without network'''
        self.config.build_policy = 'all'
        self.assertEqual(self.build(
                    packagedb_params = {'uptodate': True},
                    nonetwork = True),
                ['foo:Configuring', 'foo:Building', 'foo:Installing'])

    def test_policy_updated_with_no_network(self):
        '''Building an uptodate module with "updated" policy, without network'''
        self.config.build_policy = 'updated'
        self.assertEqual(self.build(
                    packagedb_params = {'uptodate': True},
                    nonetwork = True), [])


class TwoModulesTestCase(BuildTestCase):
    '''Building two dependent modules'''

    def setUp(self):
        super(TwoModulesTestCase, self).setUp()
        self.foo_branch = mock.Branch(os.path.join(self.config.buildroot, 'nonexistent-foo'))
        self.modules = [mock.MockModule('foo', branch=self.foo_branch),
                        mock.MockModule('bar', branch=self.branch)]
        self.modules[0].config = self.config
        self.modules[1].config = self.config

    def test_build(self):
        '''Building two autotools module'''
        self.assertEqual(self.build(),
                ['foo:Checking out', 'foo:Configuring',
                 'foo:Building', 'foo:Installing',
                 'bar:Checking out', 'bar:Configuring',
                 'bar:Building', 'bar:Installing',
                ])

    def test_build_failure_independent_modules(self):
        '''Building two independent autotools modules, with failure in first'''

        def build_error(buildscript, *args):
            self.modules[0].do_build_orig(buildscript, *args)
            raise CommandError('Mock Command Error Exception')
        build_error.depends = self.modules[0].do_build.depends
        build_error.error_phases = self.modules[0].do_build.error_phases
        self.modules[0].do_build_orig = self.modules[0].do_build
        self.modules[0].do_build = build_error

        self.assertEqual(self.build(),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building [error]',
                 'bar:Checking out', 'bar:Configuring',
                 'bar:Building', 'bar:Installing',
                ])

    def test_build_failure_dependent_modules(self):
        '''Building two dependent autotools modules, with failure in first'''
        self.modules[1].dependencies = ['foo']

        def build_error(buildscript, *args):
            self.modules[0].do_build_orig(buildscript, *args)
            raise CommandError('Mock Command Error Exception')
        build_error.depends = self.modules[0].do_build.depends
        build_error.error_phases = self.modules[0].do_build.error_phases
        self.modules[0].do_build_orig = self.modules[0].do_build
        self.modules[0].do_build = build_error

        self.assertEqual(self.build(),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building [error]'])

    def test_build_failure_dependent_modules_nopoison(self):
        '''Building two dependent autotools modules, with failure, but nopoison'''
        self.modules[1].dependencies = ['foo']

        def build_error(buildscript, *args):
            self.modules[0].do_build_orig(buildscript, *args)
            raise CommandError('Mock Command Error Exception')
        build_error.depends = self.modules[0].do_build.depends
        build_error.error_phases = self.modules[0].do_build.error_phases
        self.modules[0].do_build_orig = self.modules[0].do_build
        self.modules[0].do_build = build_error

        self.assertEqual(self.build(nopoison = True),
                ['foo:Checking out', 'foo:Configuring', 'foo:Building [error]',
                 'bar:Checking out', 'bar:Configuring',
                 'bar:Building', 'bar:Installing',
                ])

    def test_build_no_update(self):
        '''Building two uptodate, autotools module'''
        self.build() # will feed PackageDB
        self.assertEqual(self.build(),
                ['foo:Checking out', 'foo:Configuring',
                 'foo:Building', 'foo:Installing',
                 'bar:Checking out', 'bar:Configuring',
                 'bar:Building', 'bar:Installing',
                ])

    def test_build_no_update_updated_policy(self):
        '''Building two uptodate, autotools module, with 'updated' policy'''
        self.build() # will feed PackageDB
        self.assertEqual(self.build(build_policy = 'updated'),
                ['foo:Checking out', 'bar:Checking out'])

    def test_build_no_update_updated_deps_policy(self):
        '''Building two autotools module, (changed and not), with 'updated-deps' policy'''
        self.modules[1].dependencies = ['foo']
        self.build() # will feed PackageDB
        self.buildscript.packagedb.remove('foo')
        self.buildscript.packagedb.time_delta = 5
        self.assertEqual(self.build(build_policy = 'updated-deps'),
                ['foo:Checking out', 'foo:Configuring',
                 'foo:Building', 'foo:Installing',
                 'bar:Checking out', 'bar:Configuring',
                 'bar:Building', 'bar:Installing',
                ])

    def test_make_check_failure_dependent_modules(self):
        '''Building two dependent autotools modules, with failure in make check'''
        self.modules[1].dependencies = ['foo']

        def check_error(buildscript, *args):
            self.modules[0].do_check_orig(buildscript, *args)
            raise CommandError('Mock Command Error Exception')
        check_error.depends = self.modules[0].do_check.depends
        check_error.error_phases = self.modules[0].do_check.error_phases
        self.modules[0].do_check_orig = self.modules[0].do_check
        self.modules[0].do_check = check_error

        self.assertEqual(self.build(makecheck = True),
                ['foo:Checking out', 'foo:Configuring',
                 'foo:Building', 'foo:Checking [error]'])

    def test_make_check_failure_dependent_modules_makecheck_advisory(self):
        '''Building two dependent autotools modules, with *advisory* failure in make check'''
        self.modules[1].dependencies = ['foo']

        def check_error(buildscript, *args):
            buildscript.execute_is_failure = True
            try:
                self.modules[0].do_check_orig(buildscript, *args)
            finally:
                buildscript.execute_is_failure = False
        check_error.depends = self.modules[0].do_check.depends
        check_error.error_phases = self.modules[0].do_check.error_phases
        self.modules[0].do_check_orig = self.modules[0].do_check
        self.modules[0].do_check = check_error

        self.assertEqual(self.build(makecheck = True, makecheck_advisory = True),
                ['foo:Checking out', 'foo:Configuring',
                 'foo:Building', 'foo:Checking', 'foo:Installing',
                 'bar:Checking out', 'bar:Configuring',
                 'bar:Building', 'bar:Checking', 'bar:Installing'])


class SimpleBranch(object):

    def __init__(self, name, dir_path):
        self.branchname = name
        self.srcdir = dir_path
        self.checkoutdir = None

    def checkout(self, buildscript):
        pass

    def may_checkout(self, buildscript):
        return True

    def tree_id(self):
        return 'made-up-tree-id'

    def get_module_basename(self):
        return 'made-up-module-basename'


def restore_environ(env):
    # os.environ.clear() doesn't appear to change underlying environment.
    for key in os.environ.keys():
        del os.environ[key]
    for key, value in iteritems(env):
        os.environ[key] = value


STDOUT_FILENO = 1

def with_stdout_hidden(func):
    null_device = '/dev/null'
    if sys.platform.startswith('win'):
        null_device = 'NUL'
    old_fd = os.dup(STDOUT_FILENO)
    new_fd = os.open(null_device, os.O_WRONLY)
    os.dup2(new_fd, STDOUT_FILENO)
    os.close(new_fd)
    try:
        return func()
    finally:
        os.dup2(old_fd, STDOUT_FILENO)
        os.close(old_fd)


class EndToEndTest(JhbuildConfigTestCase):

    # FIXME: broken under Win32
    #def test_distutils(self):
    #    config = self.make_config()
    #    module_list = [DistutilsModule('hello',
    #                                   self.make_branch(config, 'distutils'))]
    #    module_list[0].config = self.config
    #    module_list[0].python = 'python3'
    #    build = self.make_terminal_buildscript(config, module_list)
    #    with_stdout_hidden(build.build)
    #    proc = subprocess.Popen(['hello'], stdout=subprocess.PIPE)
    #    stdout, stderr = proc.communicate()
    #    self.assertEqual(stdout.strip(), b'Hello world (distutils)')
    #    self.assertEqual(proc.wait(), 0)

    def test_autotools(self):
        config = self.make_config()
        module_list = [AutogenModule('hello',
                                     branch=self.make_branch(config, 'autotools'))]
        module_list[0].config = self.config
        build = self.make_terminal_buildscript(config, module_list)
        with_stdout_hidden(build.build)
        proc = subprocess.Popen(['hello'], stdout=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        self.assertEqual(stdout.strip(), b'Hello world (autotools)')
        self.assertEqual(proc.wait(), 0)

    # Won't pass under stock MSYS because pkgconfig isn't installed in base
    # path. Will work if you set ACLOCAL_FLAGS, PATH and PKG_CONFIG_PATH to
    # a prefix where pkg-config is installed.
    def test_autotools_with_libtool(self):
        config = self.make_config()
        module_list = [
            AutogenModule('libhello', branch=self.make_branch(config, 'libhello')),
            AutogenModule('hello', branch=self.make_branch(config, 'hello'))]
        module_list[0].config = self.config
        module_list[1].config = self.config
        build = self.make_terminal_buildscript(config, module_list)
        with_stdout_hidden(build.build)
        proc = subprocess.Popen(['hello'], stdout=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        self.assertEqual(stdout.strip(), b'Hello world (library test)')
        self.assertEqual(proc.wait(), 0)

class UtilsTest(JhbuildConfigTestCase):

    def test_compare_version(self):
        self.assertTrue(jhbuild.utils.cmds.compare_version('3.13.1.with.ckbi.1.88', '3'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('3.13.1.with.ckbi.1.88', '3.12'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('3.13.1.with.ckbi.1.88', '3.13.1'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('3.13.1.with.ckbi.1.88', '4'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('3.13.1.with.ckbi.1.88', '3.14'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('3.13.1.with.ckbi.1.88', '3.13.2'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('3with', '3.1'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('3with', '2'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('with3', '3.1'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('with3', '2'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('3.with', '3.1'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('3.with', '3'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('0.5', '0.6'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('0.5', '0.5'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('1', '1.2.3.4'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('1.2.3.4', '1'))
        self.assertTrue(jhbuild.utils.cmds.compare_version('2', '1.2.3.4'))
        self.assertFalse(jhbuild.utils.cmds.compare_version('1.2.3.4', '2'))

def get_installed_pkgconfigs(config):
    ''' overload jhbuild.utils.get_installed_pkgconfigs'''
    return {'syspkgalpha'   : '2',
            'syspkgbravo'   : '3.4'}

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    unittest.main()