Ejemplo n.º 1
0
  def test_on_context_exit(self):
    with temporary_dir() as root_dir:
      parse_context = ParseContext(create_buildfile(root_dir, 'a'))
      with pytest.raises(parse_context.ContextError):
        parse_context.on_context_exit(lambda: 37)

    with temporary_dir() as root_dir:
      buildfile = create_buildfile(root_dir, 'a',
        content=dedent("""
          import os
          from twitter.pants.base import ParseContext
          def leave_a_trail(file, contents=''):
            with open(file, 'w') as b:
              b.write(contents)
          b_file = os.path.join(os.path.dirname(__file__), 'b')
          ParseContext.locate().on_context_exit(leave_a_trail, b_file, contents='42')
          assert not os.path.exists(b_file), 'Expected context exit action to be delayed.'
        """).strip()
      )
      b_file = os.path.join(root_dir, 'a', 'b')
      self.assertFalse(os.path.exists(b_file))
      ParseContext(buildfile).parse()
      with open(b_file, 'r') as b:
        self.assertEquals('42', b.read())
Ejemplo n.º 2
0
    def test_on_context_exit(self):
        with temporary_dir() as root_dir:
            parse_context = ParseContext(create_buildfile(root_dir, 'a'))
            with pytest.raises(parse_context.ContextError):
                parse_context.on_context_exit(lambda: 37)

        with temporary_dir() as root_dir:
            buildfile = create_buildfile(root_dir,
                                         'a',
                                         content=dedent("""
          import os
          from twitter.pants.base.parse_context import ParseContext
          def leave_a_trail(file, contents=''):
            with open(file, 'w') as b:
              b.write(contents)
          b_file = os.path.join(os.path.dirname(__file__), 'b')
          ParseContext.locate().on_context_exit(leave_a_trail, b_file, contents='42')
          assert not os.path.exists(b_file), 'Expected context exit action to be delayed.'
        """).strip())
            b_file = os.path.join(root_dir, 'a', 'b')
            self.assertFalse(os.path.exists(b_file))
            ParseContext(buildfile).parse()
            with open(b_file, 'r') as b:
                self.assertEquals('42', b.read())