<commit_before># ==================================================================================================
# Copyright 2011 Twitter, Inc.
# --------------------------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or at:
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================================

import os
import pytest
import unittest

from textwrap import dedent

from twitter.common.contextutil import temporary_dir
from twitter.common.dirutil import safe_mkdir

from twitter.pants.base.address import Address
from twitter.pants.base.build_file import BuildFile
from twitter.pants.base.parse_context import ParseContext
from twitter.pants.base.target import Target

def create_buildfile(root_dir, relpath, name='BUILD', content=''):
  path = os.path.join(root_dir, relpath)
  safe_mkdir(path)
  buildfile = os.path.join(path, name)
  with open(buildfile, 'a') as f:
    f.write(content)
  return BuildFile(root_dir, relpath)


class ParseContextTest(unittest.TestCase):
  def test_locate(self):
    with pytest.raises(ParseContext.ContextError):
      ParseContext.locate()

    with temporary_dir() as root_dir:
      a_context = ParseContext(create_buildfile(root_dir, 'a'))
      b_context = ParseContext(create_buildfile(root_dir, 'b'))

      def test_in_a():
        self.assertEquals(a_context, ParseContext.locate())
        return b_context.do_in_context(lambda: ParseContext.locate())

      self.assertEquals(b_context, a_context.do_in_context(test_in_a))

  def test_parse(self):
    with temporary_dir() as root_dir:
      buildfile = create_buildfile(root_dir, 'a',
        content=dedent("""
          with open('%s/a/b', 'w') as b:
            b.write('<NAME>')
        """ % root_dir).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('<NAME>', b.read())

  def test_on_context_exit(self):<commit_msg>Fixup bad merge, ParseContextTest needs to extend BaseBuildRootTest to properly handle config loading.<commit_after># ==================================================================================================
# Copyright 2011 Twitter, Inc.
# --------------------------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or at:
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================================

import os
import pytest

from textwrap import dedent

from twitter.common.contextutil import temporary_dir
from twitter.common.dirutil import safe_mkdir

from twitter.pants.base.address import Address
from twitter.pants.base.build_file import BuildFile
from twitter.pants.base_build_root_test import BaseBuildRootTest
from twitter.pants.base.parse_context import ParseContext
from twitter.pants.base.target import Target


def create_buildfile(root_dir, relpath, name='BUILD', content=''):
  path = os.path.join(root_dir, relpath)
  safe_mkdir(path)
  buildfile = os.path.join(path, name)
  with open(buildfile, 'a') as f:
    f.write(content)
  return BuildFile(root_dir, relpath)


class ParseContextTest(BaseBuildRootTest):
  def test_locate(self):
    with pytest.raises(ParseContext.ContextError):
      ParseContext.locate()

    with temporary_dir() as root_dir:
      a_context = ParseContext(create_buildfile(root_dir, 'a'))
      b_context = ParseContext(create_buildfile(root_dir, 'b'))

      def test_in_a():
        self.assertEquals(a_context, ParseContext.locate())
        return b_context.do_in_context(lambda: ParseContext.locate())

      self.assertEquals(b_context, a_context.do_in_context(test_in_a))

  def test_parse(self):
    with temporary_dir() as root_dir:
      buildfile = create_buildfile(root_dir, 'a',
        content=dedent("""
          with open('%s/a/b', 'w') as b:
            b.write('<NAME>')
        """ % root_dir).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('<NAME>', b.read())

  def test_on_context_exit(self):
{'fraction_non_alphanumeric': 0.20239655495225614, 'fraction_numerical': 0.002995693690320165, 'mean_word_length': 3.7526690391459074, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '132073', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1450, 'n_words': 415}