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