Skip to content

Commit c3674bf

Browse files
add extensive tests for 'west init' and 'west init --topdir'
multiple combinations of arguments are tested as they can be provided to 'west init'.
1 parent c98ee20 commit c3674bf

File tree

1 file changed

+182
-1
lines changed

1 file changed

+182
-1
lines changed

tests/test_project.py

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,6 @@ def test_update_some_with_imports(repos_tmpdir):
957957
''',
958958
},
959959
)
960-
961960
cmd(['init', '-l', manifest_repo])
962961

963962
# Updating unknown projects should fail as always.
@@ -1876,6 +1875,188 @@ def test_init_local_with_manifest_filename(repos_tmpdir):
18761875
cmd('update')
18771876

18781877

1878+
TEST_CASES_INIT = [
1879+
# (local_dir, topdir, directory, manifest_file, expected_error)
1880+
######################################
1881+
# REMOTE MANIFEST
1882+
######################################
1883+
# init from remote repository (without any parameters)
1884+
(None, None, None, None, None),
1885+
# specify topdir in current directory
1886+
(None, Path('.'), None, None, None),
1887+
# specify topdir in a subfolder
1888+
(None, Path('subdir'), None, None, None),
1889+
# use deprecated [directory] to specify topdir
1890+
(None, None, Path('subdir'), None, None),
1891+
# specify topdir in a sibling
1892+
(None, Path('..') / 'sibling', None, None, None),
1893+
# specify topdir and [directory]
1894+
(None, Path('subdir'), Path('subdir') / 'extra' / 'level', None, None),
1895+
# error cases
1896+
# specify a non-existent manifest file
1897+
(None, Path('.'), None, Path('non-existent.yml'), SystemExit),
1898+
# specify topdir and [directory] (but directory is not inside topdir)
1899+
(None, Path('subdir'), Path('sibling'), None, SystemExit),
1900+
######################################
1901+
# LOCAL MANIFEST
1902+
######################################
1903+
# init workspace in current working directory (without --topdir)
1904+
(Path('workspace') / 'zephyr', None, Path('workspace') / 'zephyr', None, None),
1905+
# init workspace in current working directory
1906+
(Path('workspace') / 'zephyr', Path('.'), Path('workspace') / 'zephyr', None, None),
1907+
# init workspace in a subfolder of current working directory
1908+
(Path('workspace') / 'zephyr', Path('workspace'), Path('workspace') / 'zephyr', None, None),
1909+
# init workspace in current working directory by providing a manifest file
1910+
(Path('workspace') / 'zephyr', Path('.'), Path('workspace'), Path('zephyr') / 'west.yml', None),
1911+
# init workspace in itself by providing manifest file
1912+
(
1913+
Path('workspace') / 'zephyr',
1914+
Path('.'),
1915+
Path('.'),
1916+
Path('workspace') / 'zephyr' / 'west.yml',
1917+
None,
1918+
),
1919+
# init workspace in a subfolder by providing manifest file
1920+
(
1921+
Path('workspace') / 'subdir' / 'zephyr',
1922+
Path('workspace'),
1923+
Path('workspace') / 'subdir',
1924+
Path('zephyr') / 'west.yml',
1925+
None,
1926+
),
1927+
# init workspace in a subfolder by providing manifest file
1928+
(
1929+
Path('workspace') / 'subdir' / 'zephyr',
1930+
Path('workspace'),
1931+
Path('workspace'),
1932+
Path('subdir') / 'zephyr' / 'west.yml',
1933+
None,
1934+
),
1935+
# error cases
1936+
# init workspace without a directory
1937+
(Path('workspace') / 'zephyr', Path('.'), None, None, SystemExit),
1938+
# init workspace in a sibling repository path
1939+
(
1940+
Path('workspace') / 'zephyr',
1941+
Path('sibling'),
1942+
Path('workspace') / 'zephyr',
1943+
None,
1944+
SystemExit,
1945+
),
1946+
# init workspace from non-existent manifest
1947+
(
1948+
Path('workspace') / 'zephyr',
1949+
Path('.'),
1950+
Path('non-existent.yml'),
1951+
None,
1952+
SystemExit,
1953+
),
1954+
# init workspace from a manifest not inside the workspace
1955+
(Path('..') / 'zephyr', Path('.'), Path('..') / 'zephyr', None, SystemExit),
1956+
]
1957+
1958+
1959+
@pytest.mark.parametrize("test_case", TEST_CASES_INIT)
1960+
def test_init(repos_tmpdir, test_case):
1961+
zephyr_remote = repos_tmpdir / 'repos' / 'zephyr'
1962+
repos_tmpdir.chdir()
1963+
flags = []
1964+
1965+
local_dir, topdir, directory, manifest_file, expected_error = test_case
1966+
1967+
# prepare local manifest in local_dir
1968+
if local_dir:
1969+
# place the local manifest to given path
1970+
clone(str(zephyr_remote), str(local_dir))
1971+
flags += ['-l']
1972+
else:
1973+
# clone from remote manifest
1974+
flags += ["-m", zephyr_remote]
1975+
1976+
# extend west init flags according to specified test case
1977+
if topdir:
1978+
flags += ['-t', topdir]
1979+
if manifest_file:
1980+
flags += ['--mf', manifest_file]
1981+
if directory:
1982+
flags += [directory]
1983+
1984+
# initialize west workspace
1985+
if not expected_error:
1986+
cmd(['init'] + flags)
1987+
else:
1988+
cmd_raises(['init'] + flags, expected_error)
1989+
return
1990+
1991+
# go to west workspace and check for correct config
1992+
if local_dir:
1993+
# topdir is either specified or default (directory.parent)
1994+
workspace = topdir or directory.parent
1995+
else:
1996+
# topdir is either specified, directory or default (cwd)
1997+
workspace = topdir or directory or Path.cwd()
1998+
1999+
workspace_abs = workspace.absolute()
2000+
2001+
os.chdir(workspace)
2002+
actual = cmd('config manifest.path')
2003+
if local_dir:
2004+
assert Path(actual.rstrip()) == directory.relative_to(workspace)
2005+
else:
2006+
if topdir and directory:
2007+
# zephyr is cloned into specified subdirectory in workspace
2008+
expected = os.path.relpath(directory, topdir)
2009+
assert Path(actual.rstrip()) == Path(expected) / 'zephyr'
2010+
else:
2011+
# zephyr is cloned directly to workspace
2012+
assert Path(actual.rstrip()) == Path('zephyr')
2013+
2014+
manifest_file = manifest_file or Path('west.yml')
2015+
actual = cmd('config manifest.file')
2016+
assert Path(actual.rstrip()) == Path(manifest_file)
2017+
2018+
# update must run successful
2019+
cmd('update')
2020+
2021+
# init again must fail
2022+
_, stderr = cmd_raises(['init'] + flags, SystemExit)
2023+
assert "FATAL ERROR: already initialized" in stderr
2024+
2025+
# init topdir again from other directory must fail
2026+
os.chdir(workspace.parent)
2027+
_, stderr = cmd_raises(['init', '-l', '--topdir', workspace_abs, zephyr_remote], SystemExit)
2028+
assert "FATAL ERROR: already initialized" in stderr
2029+
_, stderr = cmd_raises(['init', '--topdir', workspace_abs, '-m', zephyr_remote], SystemExit)
2030+
assert "FATAL ERROR: already initialized" in stderr
2031+
2032+
2033+
def test_init_topdir_again(repos_tmpdir):
2034+
repos_tmpdir.chdir()
2035+
workspace = Path(repos_tmpdir) / 'ws'
2036+
zephyr = repos_tmpdir / 'repos' / 'zephyr'
2037+
2038+
# initial clone
2039+
cmd(['init', 'ws', '-m', zephyr])
2040+
2041+
# initialize west workspace again
2042+
_, stderr = cmd_raises(['init', 'ws', '-m', zephyr], SystemExit)
2043+
assert "FATAL ERROR: already initialized" in stderr
2044+
2045+
# initialize west workspace again
2046+
_, stderr = cmd_raises(['init', '--topdir', 'ws', '-m', zephyr], SystemExit)
2047+
assert "FATAL ERROR: already initialized" in stderr
2048+
2049+
# initialize west workspace again
2050+
os.chdir(workspace)
2051+
_, stderr = cmd_raises(['init', '--local', 'zephyr'], SystemExit)
2052+
assert "FATAL ERROR: already initialized" in stderr
2053+
2054+
# initialize west workspace again
2055+
os.chdir(workspace.parent)
2056+
_, stderr = cmd_raises(['init', '-l', '--topdir', 'ws', Path('ws') / 'zephyr'], SystemExit)
2057+
assert "FATAL ERROR: already initialized" in stderr
2058+
2059+
18792060
def test_init_local_with_empty_path(repos_tmpdir):
18802061
# Test "west init -l ." + "west update".
18812062
# Regression test for:

0 commit comments

Comments
 (0)