task_gen.py :  » Build » Waf » waf-1.5.17 » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Build » Waf 
Waf » waf 1.5.17 » test » task_gen.py
#! /usr/bin/env python
# encoding: utf-8
# Yinon Ehrlich, 2008, 2009

"""
Tests task generator...
"""

import os, unittest, shutil, tempfile
import common_test

import Constants
import Build
import Options
import Environment
import Utils
import TaskGen

class TaskGenTester(common_test.CommonTester):
  def __init__(self, methodName):
    common_test.CommonTester.__init__(self, methodName)

  def setUp(self):
    # define & create temporary testing directories
    self._test_dir_root = tempfile.mkdtemp("", ".waf-testing_")
    self._wscript_file_path = os.path.join(self._test_dir_root, Constants.WSCRIPT_FILE)
    self._source_file_path = "test.cpp"
    os.chdir(self._test_dir_root)

    # reload() is used to isolate imports between tests:
    # other tests (in other modules) call Build.new_task_gen,
    # which, in turn, creates an instance of TaskGen (without import !)
    # Python's garbage collector holds a reference to that instance,
    # thus ruins tests here.
    # to reproduce this error, delete the reload() below,
    # then call to task_gen.test_missing_mapping AFTER calling to
    # cxx_test.test_default_flags_patterns
    global TaskGen
    TaskGen=reload(TaskGen)

  def tearDown(self):
    '''tearDown - deletes the directories and files created by the tests ran'''
    os.chdir(self._waf_root_dir)

    if os.path.isdir(self._test_dir_root):
      shutil.rmtree(self._test_dir_root)

  def _write_wscript(self, contents):
    self._write_file(self._wscript_file_path, contents)

  def _write_source(self, contents):
    self._write_file(self._source_file_path, contents)

  def _write_file(self, file_path, contents):
    try:
      wscript_file = open(file_path, 'w')
      wscript_file.write(contents)
    finally:
      wscript_file.close()

  def test_black_no_sources_specified(self):
    # black-box test: fails if source not specified
    wscript_contents = """
blddir = 'build'
srcdir = '.'

def build(bld):
  obj = bld.new_task_gen('cxx', 'shlib')

def configure(conf):
  conf.check_tool('g++')
"""

    self._write_wscript(wscript_contents)
    self._test_configure()
    self._test_build(test_for_success=False)

  def test_white_no_sources_specified(self):
    # white-box test: no sources were specified

    # add apply_verif to taskgen
    import Tools.ccroot

    Options.commands['configure'] = False
    env = Environment.Environment()
    bld = Build.bld = Build.BuildContext()
    bld.set_env('default', env)
    blddir = os.path.join(self._test_dir_root, 'b')
    bld.load_dirs(self._test_dir_root, blddir)

    obj = TaskGen.task_gen(bld=bld)
    # TODO: make sure it works with apply_core too
    self.failUnlessRaises(Utils.WafError, obj.apply_verif)

  def test_source_not_found(self):
    # black-box test: fails if source not found
    wscript_contents = """
blddir = 'build'
srcdir = '.'

def build(bld):
  obj = bld.new_task_gen('cxx')
  obj.name = 'hh'
  obj.source = 'fwefwe.cpp'

def configure(conf):
  conf.check_tool('g++')
"""

    self._write_wscript(wscript_contents)
    self._test_configure()
    self._test_build(False)

  def make_bld(self):
    Options.commands['configure'] = False
    env = Environment.Environment()
    bld = Build.bld = Build.BuildContext()
    bld.set_env('default', env)
    blddir = os.path.join(self._test_dir_root, 'b')
    bld.load_dirs(self._test_dir_root, blddir)
    return bld

  def test_missing_mapping(self):
    # no mapping for extension
    bld = self.make_bld()
    obj = TaskGen.task_gen(bld=bld)
    obj.source = self._source_file_path
    self._write_source("int main() {return 0;}")
    self.failUnlessRaises(Utils.WafError, obj.apply_core)

  def test_validate_find_srcs_excs(self):
    # find sources in dirs 'excludes' must be a lst
    bld = self.make_bld()
    obj = TaskGen.task_gen(bld=bld)
    self.failUnlessRaises(Utils.WafError, obj.find_sources_in_dirs, 'a', 'excludes=b')

  def test_validate_find_srcs_exts(self):
    # find sources in dirs 'exts' must be a list
    bld = self.make_bld()
    obj = TaskGen.task_gen(bld=bld)
    self.failUnlessRaises(Utils.WafError, obj.find_sources_in_dirs, 'a', 'exts=b')

  def test_validate_find_srcs_absolute(self):
    # find sources in dirs cannot get absoulte paths
    bld = self.make_bld()
    obj = TaskGen.task_gen(bld=bld)
    self.failUnlessRaises(Utils.WafError, obj.find_sources_in_dirs, self._test_dir_root)

  def test_validate_extension_decorator(self):
    # black-box test: fails if decorator variable is not a string or a list
    wscript_contents = """
from TaskGen import extension

# this fails - extension must be a list or a string
@extension(8881)
def baba(self):
  pass
"""

    self._write_wscript(wscript_contents)
    stderr = self._test_configure(False)[1]
    err_msg = 'extension takes either a list or a string'
    self.assert_(err_msg in stderr, err_msg)

  def test_validate_declare_extension(self):
    self.failUnlessRaises(Utils.WafError, TaskGen.declare_extension, 1, None)

def run_tests(verbose=1):
  suite = unittest.TestLoader().loadTestsFromTestCase(TaskGenTester)
#   suite = unittest.TestLoader().loadTestsFromNames(['test_missing_mapping'], TaskGenTester)
#  unittest.TestLoader().sortTestMethodsUsing = None
  return unittest.TextTestRunner(verbosity=verbose).run(suite)

if __name__ == '__main__':
  # test must be ran from waf's root directory
  os.chdir(os.path.pardir)
  options = common_test.get_args_options()
  run_tests(options.verbose)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.