Skip to content

Commit

Permalink
Replace tempfile.NamedTemporaryFile with tempfile.TemporaryDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi committed Oct 4, 2022
1 parent 9f49868 commit 50be15e
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions python/test/test_action_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,22 @@ def test_get_settings(self):
options = self.do_test_get_settings()
options = {f'INPUT_{key}': value
for key, value in options.items()
if key not in {'GITHUB_API_URL', 'GITHUB_GRAPHQL_URL', 'GITHUB_SHA'}}
if key not in {'GITHUB_API_URL', 'GITHUB_GRAPHQL_URL', 'GITHUB_SHA', 'GITHUB_EVENT_PATH'}}
self.do_test_get_settings(**options)

def test_get_settings_event_file(self):
self.do_test_get_settings(expected=self.get_settings(event_file=None))
self.do_test_get_settings(EVENT_FILE='', expected=self.get_settings(event_file=None))
self.do_test_get_settings(EVENT_FILE=None, expected=self.get_settings(event_file=None))

with tempfile.NamedTemporaryFile(mode='wb', delete=sys.platform != 'win32') as file:
file.write(b'{}')
file.flush()
if sys.platform == 'win32':
file.close()
with tempfile.TemporaryDirectory() as path:
event = {"key": "val"}

filepath = os.path.join(path, 'event.json')
with open(filepath, 'wt', encoding='utf-8') as w:
w.write(json.dumps(event, ensure_ascii=False))

try:
self.do_test_get_settings(EVENT_FILE=file.name, expected=self.get_settings(event_file=file.name))
finally:
if sys.platform == 'win32':
os.unlink(file.name)
self.do_test_get_settings(EVENT_FILE=filepath, expected=self.get_settings(event=event, event_file=filepath))

def test_get_settings_github_api_url(self):
self.do_test_get_settings(GITHUB_API_URL='https://api.github.onpremise.com', expected=self.get_settings(api_url='https://api.github.onpremise.com'))
Expand Down Expand Up @@ -482,24 +479,18 @@ def do_test_get_settings_no_default_files(self,
self.do_test_get_settings(event, gha, warning=warning, expected=expected, **options)

def do_test_get_settings(self,
event: dict = {},
event: Optional[dict] = None,
gha: Optional[GithubAction] = None,
warning: Optional[Union[str, List[str]]] = None,
exception: Optional[Type[Exception]] = None,
expected: Settings = get_settings.__func__(),
**kwargs):
event = event.copy()
with tempfile.TemporaryDirectory() as path:
filepath = os.path.join(path, 'event.json')
with open(filepath, 'wt', encoding='utf-8') as w:
w.write(json.dumps(event, ensure_ascii=False))

for key in ['GITHUB_EVENT_PATH', 'INPUT_GITHUB_EVENT_PATH']:
if key in kwargs and kwargs[key]:
kwargs[key] = filepath
if event is None:
event = {}

with tempfile.TemporaryDirectory() as path:
# default options
options = dict(
GITHUB_EVENT_PATH=filepath,
GITHUB_EVENT_NAME='event name',
GITHUB_API_URL='http://github.api.url/', #defaults to github
GITHUB_GRAPHQL_URL='http://github.graphql.url/', #defaults to github
Expand All @@ -522,6 +513,15 @@ def do_test_get_settings(self,
SECONDS_BETWEEN_GITHUB_READS='1.5',
SECONDS_BETWEEN_GITHUB_WRITES='2.5'
)

# provide event via GITHUB_EVENT_PATH when there is no EVENT_FILE given
if 'EVENT_FILE' not in kwargs or not kwargs['EVENT_FILE']:
filepath = os.path.join(path, 'event.json')
with open(filepath, 'wt', encoding='utf-8') as w:
w.write(json.dumps(event, ensure_ascii=False))
options.update(GITHUB_EVENT_PATH=filepath)

# overwrite default options
options.update(**kwargs)
for arg in kwargs:
if arg.startswith('INPUT_'):
Expand All @@ -542,7 +542,7 @@ def do_test_get_settings(self,
return None

actual = get_settings(options, gha)
m.assert_called_once_with(options, event)
m.assert_called_once_with(options, expected.event)
if warning:
if isinstance(warning, list):
gha.warning.assert_has_calls([mock.call(w) for w in warning], any_order=False)
Expand Down Expand Up @@ -966,25 +966,20 @@ def test_is_float(self):
self.assertEqual(expected, is_float(value), value)

def test_main_fork_pr_check(self):
with tempfile.NamedTemporaryFile(mode='wb', delete=sys.platform != 'win32') as file:
file.write(b'{ "pull_request": { "head": { "repo": { "full_name": "fork/repo" } } } }')
file.flush()
if sys.platform == 'win32':
file.close()
with tempfile.TemporaryDirectory() as path:
filepath = os.path.join(path, 'file')
with open(filepath, 'wt', encoding='utf-8') as file:
file.write('{ "pull_request": { "head": { "repo": { "full_name": "fork/repo" } } } }')

gha = mock.MagicMock()
try:
settings = get_settings(dict(
COMMIT='commit',
GITHUB_TOKEN='********',
GITHUB_EVENT_PATH=file.name,
GITHUB_EVENT_NAME='pull_request',
GITHUB_REPOSITORY='repo',
EVENT_FILE=None
), gha)
finally:
if sys.platform == 'win32':
os.unlink(file.name)
settings = get_settings(dict(
COMMIT='commit',
GITHUB_TOKEN='********',
GITHUB_EVENT_PATH=file.name,
GITHUB_EVENT_NAME='pull_request',
GITHUB_REPOSITORY='repo',
EVENT_FILE=None
), gha)

def do_raise(*args):
# if this is raised, the tested main method did not return where expected but continued
Expand Down

0 comments on commit 50be15e

Please sign in to comment.