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