@@ -1870,6 +1870,228 @@ 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, expected_error)
1994+ ######################################
1995+ # REMOTE MANIFEST
1996+ ######################################
1997+ # specify a non-existent manifest file
1998+ (
1999+ None ,
2000+ Path ('.' ),
2001+ None ,
2002+ Path ('non-existent.yml' ),
2003+ SystemExit ,
2004+ "FATAL ERROR: can't init: no non-existent.yml found" ,
2005+ ),
2006+ # specify topdir and [directory] (but directory is not inside topdir)
2007+ (
2008+ None ,
2009+ Path ('subdir' ),
2010+ Path ('sibling' ),
2011+ None ,
2012+ SystemExit ,
2013+ "FATAL ERROR: directory 'sibling' must be relative to west topdir 'subdir'" ,
2014+ ),
2015+ ######################################
2016+ # LOCAL MANIFEST
2017+ ######################################
2018+ # init workspace without a directory
2019+ (
2020+ Path ('workspace' ) / 'zephyr' ,
2021+ Path ('.' ),
2022+ None ,
2023+ None ,
2024+ SystemExit ,
2025+ "FATAL ERROR: can't init: no west.yml found" ,
2026+ ),
2027+ # init workspace in a sibling repository path
2028+ (
2029+ Path ('workspace' ) / 'zephyr' ,
2030+ Path ('sibling' ),
2031+ Path ('workspace' ) / 'zephyr' ,
2032+ None ,
2033+ SystemExit ,
2034+ "workspace/zephyr/west.yml must be relative to west topdir" ,
2035+ ),
2036+ # init workspace from non-existent manifest
2037+ (
2038+ Path ('workspace' ) / 'zephyr' ,
2039+ Path ('.' ),
2040+ Path ('non-existent.yml' ),
2041+ None ,
2042+ SystemExit ,
2043+ "FATAL ERROR: can't init: no west.yml found" ,
2044+ ),
2045+ # init workspace from a manifest not inside the workspace
2046+ (
2047+ Path ('..' ) / 'zephyr' ,
2048+ Path ('.' ),
2049+ Path ('..' ) / 'zephyr' ,
2050+ None ,
2051+ SystemExit ,
2052+ "zephyr/west.yml must be relative to west topdir" ,
2053+ ),
2054+ ]
2055+
2056+
2057+ @pytest .mark .parametrize ("test_case" , TEST_CASES_INIT_FAILS )
2058+ def test_init_fails (repos_tmpdir , test_case ):
2059+ local_dir , topdir , directory , manifest_file , expected_error , expected_stderr = test_case
2060+
2061+ manifest_repo = repos_tmpdir / 'repos' / 'zephyr'
2062+ repos_tmpdir .chdir ()
2063+ flags = _setup_test_init (manifest_repo , local_dir , topdir , directory , manifest_file )
2064+ _ , stderr = cmd_raises (['init' ] + flags , expected_error )
2065+ assert expected_stderr in stderr
2066+
2067+
2068+ def test_init_topdir_again (repos_tmpdir ):
2069+ repos_tmpdir .chdir ()
2070+ workspace = Path (repos_tmpdir ) / 'ws'
2071+ zephyr = repos_tmpdir / 'repos' / 'zephyr'
2072+
2073+ # initial clone
2074+ cmd (['init' , 'ws' , '-m' , zephyr ])
2075+
2076+ # initialize west workspace again
2077+ _ , stderr = cmd_raises (['init' , 'ws' , '-m' , zephyr ], SystemExit )
2078+ assert "FATAL ERROR: already initialized" in stderr
2079+
2080+ # initialize west workspace again
2081+ _ , stderr = cmd_raises (['init' , '--topdir' , 'ws' , '-m' , zephyr ], SystemExit )
2082+ assert "FATAL ERROR: already initialized" in stderr
2083+
2084+ # initialize west workspace again
2085+ os .chdir (workspace )
2086+ _ , stderr = cmd_raises (['init' , '--local' , 'zephyr' ], SystemExit )
2087+ assert "FATAL ERROR: already initialized" in stderr
2088+
2089+ # initialize west workspace again
2090+ os .chdir (workspace .parent )
2091+ _ , stderr = cmd_raises (['init' , '-l' , '--topdir' , 'ws' , Path ('ws' ) / 'zephyr' ], SystemExit )
2092+ assert "FATAL ERROR: already initialized" in stderr
2093+
2094+
18732095def test_init_local_with_empty_path (repos_tmpdir ):
18742096 # Test "west init -l ." + "west update".
18752097 # Regression test for:
0 commit comments