Commit ea186522 authored by Claude Paroz's avatar Claude Paroz Committed by Tim Graham
Browse files

[1.7.x] Made model_regress unpickling test CWD-independent

Refs #24007. Thanks Tim Graham for his help with the patch.

Backport of 1d9fc5ca and
995be4a1 from master
parent a970d6d9
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -2,13 +2,14 @@ from __future__ import unicode_literals

import datetime
from operator import attrgetter
import os
import pickle
import subprocess
import sys
import tempfile
import unittest

from django.core.exceptions import ValidationError
from django.core.files.temp import NamedTemporaryFile
from django.test import TestCase, skipUnlessDBFeature
from django.utils import six
from django.utils.timezone import get_fixed_timezone
@@ -272,11 +273,19 @@ print(article.headline)"""
            article_text="This is an article",
        )

        with tempfile.NamedTemporaryFile(mode='w+', suffix=".py", dir='.', delete=True) as script:
        with NamedTemporaryFile(mode='w+', suffix=".py", dir='.') as script:
            script.write(script_template % pickle.dumps(a))
            script.flush()
            try:
                result = subprocess.check_output(['python', script.name])
                result = subprocess.check_output(
                    [sys.executable, script.name],
                    env={
                        # Needed to run test outside of tests directory
                        str('PYTHONPATH'): os.pathsep.join(sys.path),
                        # Needed on Windows because http://bugs.python.org/issue8557
                        str('PATH'): os.environ['PATH'],
                    }
                )
            except subprocess.CalledProcessError:
                self.fail("Unable to reload model pickled data")
        self.assertEqual(result.strip().decode(), "Some object")