Skip to content

Commit

Permalink
Handle empty .stestr directory to initialize
Browse files Browse the repository at this point in the history
This commit make an empty .stestr directory initialize if it exists.
Originally, we make it errors or weird errors occur when there is.
However, an empty .stestr directory can be initialized and no error
shouldn't be occurred with it.

Fixes: mtreinish#266
  • Loading branch information
masayukig committed Sep 19, 2019
1 parent fd2cfb8 commit 834b6f1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion stestr/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

"""Initialise a new repository."""

import errno
import sys

from cliff import command
Expand Down Expand Up @@ -47,7 +48,7 @@ def init(repo_type='file', repo_url=None, stdout=sys.stdout):
try:
util.get_repo_initialise(repo_type, repo_url)
except OSError as e:
if e.errno != 17:
if e.errno != errno.EEXIST:
raise
repo_path = repo_url or './stestr'
stdout.write('The specified repository directory %s already exists. '
Expand Down
15 changes: 13 additions & 2 deletions stestr/commands/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Load data into a repository."""


import errno
import functools
import os
import sys
Expand Down Expand Up @@ -148,7 +149,7 @@ def load(force_init=False, in_streams=None,
used by the run command to both output the results as well as store the
result in the repository.
:param bool force_init: Initialize the specifiedrepository if it hasn't
:param bool force_init: Initialize the specified repository if it hasn't
been created.
:param list in_streams: A list of file objects that will be saved into the
repository
Expand Down Expand Up @@ -184,7 +185,17 @@ def load(force_init=False, in_streams=None,
repo = util.get_repo_open(repo_type, repo_url)
except repository.RepositoryNotFound:
if force_init:
repo = util.get_repo_initialise(repo_type, repo_url)
try:
repo = util.get_repo_initialise(repo_type, repo_url)
except OSError as e:
if e.errno != errno.EEXIST:
raise
repo_path = repo_url or './stestr'
stdout.write('The specified repository directory %s already '
'exists. Please check if the repository already '
'exists or select a different path\n'
% repo_path)
exit(1)
else:
raise
# Not a full implementation of TestCase, but we only need to iterate
Expand Down
13 changes: 12 additions & 1 deletion stestr/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

"""Run a projects tests and load them into stestr."""

import errno
import os
import subprocess
import sys
Expand Down Expand Up @@ -354,7 +355,17 @@ def run_command(config='.stestr.conf', repo_type='file',
"--test-path ")
stdout.write(msg)
exit(1)
repo = util.get_repo_initialise(repo_type, repo_url)
try:
repo = util.get_repo_initialise(repo_type, repo_url)
except OSError as e:
if e.errno != errno.EEXIST:
raise
repo_path = repo_url or './stestr'
stdout.write('The specified repository directory %s already '
'exists. Please check if the repository already '
'exists or select a different path\n' % repo_path)
return 1

combine_id = None
concurrency = _to_int(concurrency)

Expand Down
9 changes: 8 additions & 1 deletion stestr/repository/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ class RepositoryFactory(repository.AbstractRepositoryFactory):
def initialise(klass, url):
"""Create a repository at url/path."""
base = os.path.join(os.path.expanduser(url), '.stestr')
os.mkdir(base)
try:
os.mkdir(base)
except OSError as e:
if e.errno == errno.EEXIST and not os.listdir(base):
# It shouldn't be harmful initializing an empty dir
pass
else:
raise
with open(os.path.join(base, 'format'), 'wt') as stream:
stream.write('1\n')
result = Repository(base)
Expand Down

0 comments on commit 834b6f1

Please sign in to comment.