Skip to content

Commit

Permalink
Stop using random temp folders (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiasli authored Mar 10, 2020
1 parent 34aed7d commit f539a58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
16 changes: 14 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@
import os
import stat
import unittest
import tempfile
import shutil
try:
import mock
except ImportError:
from unittest import mock
from six.moves import configparser

from knack.config import CLIConfig, get_config_parser
from .util import TEMP_FOLDER_NAME, new_temp_folder


def clean_local_temp_folder():
local_temp_folders = os.path.join(os.getcwd(), TEMP_FOLDER_NAME)
if os.path.exists(local_temp_folders):
shutil.rmtree(local_temp_folders)


class TestCLIConfig(unittest.TestCase):

def setUp(self):
self.cli_config = CLIConfig(config_dir=tempfile.mkdtemp())
self.cli_config = CLIConfig(config_dir=new_temp_folder())
# In case the previous test is stopped and doesn't clean up
clean_local_temp_folder()

def tearDown(self):
clean_local_temp_folder()

def test_has_option(self):
section = 'MySection'
Expand Down
17 changes: 15 additions & 2 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
from unittest import mock
import sys
import tempfile
import shutil
import os
from six import StringIO

from knack.cli import CLI, CLICommandsLoader, CommandInvoker

TEMP_FOLDER_NAME = "knack_temp"


def redirect_io(func):

original_stderr = sys.stderr
Expand All @@ -30,7 +35,7 @@ def wrapper(self):
class MockContext(CLI):

def __init__(self):
super(MockContext, self).__init__(config_dir=tempfile.mkdtemp())
super(MockContext, self).__init__(config_dir=new_temp_folder())
loader = CLICommandsLoader(cli_ctx=self)
invocation = mock.MagicMock(spec=CommandInvoker)
invocation.data = {}
Expand All @@ -44,5 +49,13 @@ def get_cli_version(self):
return '0.1.0'

def __init__(self, **kwargs):
kwargs['config_dir'] = tempfile.mkdtemp()
kwargs['config_dir'] = new_temp_folder()
super(DummyCLI, self).__init__(**kwargs)


def new_temp_folder():
temp_dir = os.path.join(tempfile.gettempdir(), TEMP_FOLDER_NAME)
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
os.mkdir(temp_dir)
return temp_dir

0 comments on commit f539a58

Please sign in to comment.