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