<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys

import attr
import six

import pytest
from _pytest import pathlib
from _pytest.pathlib import Path
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG


def test_tmpdir_fixture(testdir):
    p = testdir.copy_example("tmpdir/tmpdir_fixture.py")
    results = testdir.runpytest(p)
    results.stdout.fnmatch_lines("*1 passed*")


def test_ensuretemp(recwarn):
    d1 = pytest.ensuretemp("hello")
    d2 = pytest.ensuretemp("hello")
    assert d1 == d2
    assert d1.check(dir=1)


@attr.s
class FakeConfig(object):
    basetemp = attr.ib()
    trace = attr.ib(default=None)

    @property
    def trace(self):
        return self

    def get(self, key):
        return lambda *k: None

    @property
    def option(self):
        return self


class TestTempdirHandler(object):
    def test_mktemp(self, tmp_path):

        from _pytest.tmpdir import TempdirFactory, TempPathFactory

        config = FakeConfig(tmp_path)
        t = TempdirFactory(TempPathFactory.from_config(config))
        tmp = t.mktemp("world")
        assert tmp.relto(t.getbasetemp()) == "world0"
        tmp = t.mktemp("this")
        assert tmp.relto(t.getbasetemp()).startswith("this")
        tmp2 = t.mktemp("this")
        assert tmp2.relto(t.getbasetemp()).startswith("this")
        assert tmp2 != tmp

    @pytest.mark.issue(4425)
    def test_tmppath_relative_basetemp_absolute(self, tmp_path, monkeypatch):
        from _pytest.tmpdir import TempPathFactory

        monkeypatch.chdir(tmp_path)
        config = FakeConfig("hello")
        t = TempPathFactory.from_config(config)
        assert t.getbasetemp() == (tmp_path / "hello")


class TestConfigTmpdir(object):
    def test_getbasetemp_custom_removes_old(self, testdir):
        mytemp = testdir.tmpdir.join("xyz")
        p = testdir.makepyfile(
            """
            def test_1(tmpdir):
                pass
        """
        )
        testdir.runpytest(p, "--basetemp=%s" % mytemp)
        mytemp.check()
        mytemp.ensure("hello")

        testdir.runpytest(p, "--basetemp=%s" % mytemp)
        mytemp.check()
        assert not mytemp.join("hello").check()


def test_basetemp(testdir):
    mytemp = testdir.tmpdir.mkdir("mytemp")
    p = testdir.makepyfile(
        """
        import pytest
        def test_1():
            pytest.ensuretemp("hello")
    """
    )
    result = testdir.runpytest(p, "--basetemp=%s" % mytemp, SHOW_PYTEST_WARNINGS_ARG)
    assert result.ret == 0<commit_msg>use Path.resolve in test to sort out osx temporary folder being a symlink<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys

import attr
import six

import pytest
from _pytest import pathlib
from _pytest.pathlib import Path
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG


def test_tmpdir_fixture(testdir):
    p = testdir.copy_example("tmpdir/tmpdir_fixture.py")
    results = testdir.runpytest(p)
    results.stdout.fnmatch_lines("*1 passed*")


def test_ensuretemp(recwarn):
    d1 = pytest.ensuretemp("hello")
    d2 = pytest.ensuretemp("hello")
    assert d1 == d2
    assert d1.check(dir=1)


@attr.s
class FakeConfig(object):
    basetemp = attr.ib()
    trace = attr.ib(default=None)

    @property
    def trace(self):
        return self

    def get(self, key):
        return lambda *k: None

    @property
    def option(self):
        return self


class TestTempdirHandler(object):
    def test_mktemp(self, tmp_path):

        from _pytest.tmpdir import TempdirFactory, TempPathFactory

        config = FakeConfig(tmp_path)
        t = TempdirFactory(TempPathFactory.from_config(config))
        tmp = t.mktemp("world")
        assert tmp.relto(t.getbasetemp()) == "world0"
        tmp = t.mktemp("this")
        assert tmp.relto(t.getbasetemp()).startswith("this")
        tmp2 = t.mktemp("this")
        assert tmp2.relto(t.getbasetemp()).startswith("this")
        assert tmp2 != tmp

    @pytest.mark.issue(4425)
    def test_tmppath_relative_basetemp_absolute(self, tmp_path, monkeypatch):
        from _pytest.tmpdir import TempPathFactory

        monkeypatch.chdir(tmp_path)
        config = FakeConfig("hello")
        t = TempPathFactory.from_config(config)
        assert t.getbasetemp().resolve() == (tmp_path / "hello").resolve()


class TestConfigTmpdir(object):
    def test_getbasetemp_custom_removes_old(self, testdir):
        mytemp = testdir.tmpdir.join("xyz")
        p = testdir.makepyfile(
            """
            def test_1(tmpdir):
                pass
        """
        )
        testdir.runpytest(p, "--basetemp=%s" % mytemp)
        mytemp.check()
        mytemp.ensure("hello")

        testdir.runpytest(p, "--basetemp=%s" % mytemp)
        mytemp.check()
        assert not mytemp.join("hello").check()


def test_basetemp(testdir):
    mytemp = testdir.tmpdir.mkdir("mytemp")
    p = testdir.makepyfile(
        """
        import pytest
        def test_1():
            pytest.ensuretemp("hello")
    """
    )
    result = testdir.runpytest(p, "--basetemp=%s" % mytemp, SHOW_PYTEST_WARNINGS_ARG)
    assert result.ret == 0
{'fraction_non_alphanumeric': 0.11231470923603193, 'fraction_numerical': 0.0068415051311288486, 'mean_word_length': 2.86417033773862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4190440', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1757, 'n_words': 343}