def test_primitive(): class NoPrimitive(Anod): def build(self): return 2 no_primitive = NoPrimitive("", "build") assert has_primitive(no_primitive, "build") is False class WithPrimitive(Anod): build_qualifier_format = (("error", False),) package = Anod.Package(prefix="mypackage", version=lambda: "42") @Anod.primitive() def build(self): if "error" in self.parsed_qualifier: raise ValueError(self.parsed_qualifier["error"]) elif "error2" in self.parsed_qualifier: self.shell(sys.executable, "-c", "import sys; sys.exit(2)") else: hello = self.shell( sys.executable, "-c", 'print("world")', output=subprocess.PIPE ) return hello.out.strip() with_primitive = WithPrimitive("", "build") with_primitive2 = WithPrimitive("error=foobar", "build") with_primitive3 = WithPrimitive("error2", "build") with_primitive4 = WithPrimitive("error3", "build") Anod.sandbox = SandBox(root_dir=os.getcwd()) Anod.sandbox.spec_dir = os.path.join(os.path.dirname(__file__), "data") Anod.sandbox.create_dirs() # Activate the logging AnodDriver(anod_instance=with_primitive, store=None).activate(Anod.sandbox, None) AnodDriver(anod_instance=with_primitive2, store=None).activate(Anod.sandbox, None) AnodDriver(anod_instance=with_primitive3, store=None).activate(Anod.sandbox, None) AnodDriver(anod_instance=with_primitive4, store=None) # don't activate with_primitive.build_space.create() assert has_primitive(with_primitive, "build") is True assert with_primitive.build() == "world" with_primitive2.build_space.create() with pytest.raises(AnodError): with_primitive2.build() assert with_primitive2.package.name.startswith("mypackage") # Check __getitem__ # PKG_DIR returns the path to the pkg directory assert with_primitive2["PKG_DIR"].endswith("pkg") with_primitive3.build_space.create() with pytest.raises(AnodError): with_primitive3.build()
def test_custom_repr(): """Test yaml custom repr for Make.""" Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = Anod(qualifier='', kind='build', jobs=1) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) m = Make(ac, exec_dir='/here', makefile='/tmp/makefile') AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() assert "cmd: [make, -f, /tmp/makefile, -j, '1']" in yaml.dump(m)
def test_configure(): class AnodConf(Anod): @Anod.primitive() def build(self): c = Configure(self) return c.cmdline() Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = AnodConf(qualifier="", kind="build", jobs=10) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() # Configure() can add $CONFIG_SHELL in the command line # Check that the two other arguments are as expected assert ac.build()["cmd"][-2:] == [ "../src/configure", "--build=%s" % ac.env.build.triplet, ] # Check with canadian env canadian_env = BaseEnv() canadian_env.set_build("x86-windows") canadian_env.set_host("x86-linux") canadian_env.set_target("arm-elf") assert canadian_env.is_canadian ac2 = AnodConf(qualifier="", kind="build", jobs=10, env=canadian_env) AnodDriver(anod_instance=ac2, store=None).activate(Anod.sandbox, None) ac2.build_space.create() ac2_cmd = ac2.build()["cmd"] assert "--build=i686-pc-mingw32" in ac2_cmd assert "--host=i686-pc-linux-gnu" in ac2_cmd assert "--target=arm-eabi" in ac2_cmd # Check with cross env cross_env = BaseEnv() cross_env.set_target("arm-elf") ac3 = AnodConf(qualifier="", kind="build", jobs=10, env=cross_env) AnodDriver(anod_instance=ac3, store=None).activate(Anod.sandbox, None) ac3.build_space.create() assert "--target=arm-eabi" in ac3.build()["cmd"]
def test_configure_opts(): """Check configure options.""" class AnodConf(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): c = Configure(self) c.add("--with-opt") c.add_env("OPT", "VAL") return [c.cmdline(), c()] os.environ["CONFIG_SHELL"] = "ksh" Anod.sandbox = SandBox(root_dir=os.getcwd()) Anod.sandbox.create_dirs() ac = AnodConf(qualifier="", kind="build", jobs=10) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() result = ac.build() assert result[0]["cmd"][:-1] == ["ksh", "../src/configure", "--with-opt"] assert result[0]["options"]["env"] == {"OPT": "VAL"} assert result[1][0][:-1] == tuple(result[0]["cmd"][:-1]) assert result[1][1]["env"] == result[0]["options"]["env"]
def test_make(): class AnodMake(Anod): @Anod.primitive() def build(self): m1 = Make(self, makefile='/tmp/makefile') m1.set_var('prefix', '/foo') m2 = Make(self, jobs=2) m2.set_default_target('install') m2.set_var('profiles', ['dev', 'prod']) return (m1.cmdline()['cmd'], m2.cmdline()['cmd'], m2.cmdline(['clean', 'install'])['cmd'], m2.cmdline('all')['cmd']) Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() am = AnodMake(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=am, store=None).activate() am.build_space.create() assert am.build() == ([ 'make', '-f', '/tmp/makefile', '-j', '10', 'prefix=/foo' ], ['make', '-j', '2', 'profiles=dev prod', 'install' ], ['make', '-j', '2', 'profiles=dev prod', 'clean', 'install'], ['make', '-j', '2', 'profiles=dev prod', 'all'])
def test_configure_opts(): """Check configure options.""" class AnodConf(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): c = Configure(self) c.add('--with-opt') c.add_env('OPT', 'VAL') return [c.cmdline(), c()] os.environ['CONFIG_SHELL'] = 'ksh' Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = AnodConf(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=ac, store=None).activate(Anod.sandbox, None) ac.build_space.create() result = ac.build() assert result[0]['cmd'][:-1] == [ 'ksh', '../src/configure', '--with-opt'] assert result[0]['options']['env'] == {'OPT': 'VAL'} assert result[1][0][:-1] == tuple(result[0]['cmd'][:-1]) assert result[1][1]['env'] == result[0]['options']['env']
def run_anod_primitive(self, primitive: str) -> None: """Run an anod primitive after setting up the sandbox.""" self.data.anod_instance.sandbox = self.sandbox anod_driver = AnodDriver(anod_instance=self.data.anod_instance, store=self.store) anod_driver.activate(self.data.anod_instance.sandbox, self.spec_repo) anod_driver.anod_instance.build_space.create(quiet=True) getattr(anod_driver.anod_instance, primitive)() self.__status = STATUS.success
def do_installsource(self): """Install the source from tmp/cache to build_space/src.""" spec = self.data.spec spec.sandbox = self.sandbox anod_instance = AnodDriver(anod_instance=spec, store=self.store) anod_instance.activate() source = self.data.source src_dir = os.path.join(self.sandbox.tmp_dir, 'cache', source.name) dest_dir = os.path.join(spec.build_space.src_dir) sync_tree(src_dir, dest_dir, ignore=source.ignore) self.status = STATUS.success
def do_test(self): """Run the test primitive.""" self.data.anod_instance.sandbox = self.sandbox anod_driver = AnodDriver(anod_instance=self.data.anod_instance, store=self.store) anod_driver.activate() anod_driver.anod_instance.build_space.create(quiet=True) if getattr(anod_driver.anod_instance, 'test', None) is None: logger.error('primitive test not implemented in the spec %s', self.data.anod_instance) self.status = STATUS.failure return anod_driver.anod_instance.test() self.status = STATUS.success
def do_build(self): """Run the build primitive after setting up the sandbox.""" self.data.anod_instance.sandbox = self.sandbox anod_driver = AnodDriver(anod_instance=self.data.anod_instance, store=self.store) anod_driver.activate() anod_driver.anod_instance.build_space.create(quiet=True) if getattr(anod_driver.anod_instance, 'build', None) is None: logger.error('primitive build not implemented in the spec %s', self.data.anod_instance) self.status = STATUS.failure return anod_driver.anod_instance.build() self.status = STATUS.success
def do_installsource(self) -> None: """Install the source from tmp/cache to build_space/src.""" spec = self.data.spec spec.sandbox = self.sandbox anod_instance = AnodDriver(anod_instance=spec, store=self.store) anod_instance.activate(self.sandbox, self.spec_repo) source = self.data.source src_dir = os.path.join(self.sandbox.tmp_dir, "cache", source.name) if not source.dest: dest_dir = spec.build_space.src_dir else: dest_dir = os.path.join(spec.build_space.src_dir, source.dest) if not os.path.isdir(src_dir): # defensive code logger.critical("source directory %s does not exist", src_dir) self.__status = STATUS.failure return sync_tree(src_dir, dest_dir, ignore=source.ignore) self.__status = STATUS.success
def test_configure_opts(): """Check configure options.""" class AnodConf(Anod): @Anod.primitive() def build(self): c = Configure(self) c.add('--with-opt') c.add_env('OPT', 'VAL') return c.cmdline() os.environ['CONFIG_SHELL'] = 'ksh' Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() ac = AnodConf(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=ac, store=None).activate() ac.build_space.create() assert ac.build()['cmd'][:-1] == ['ksh', '../src/configure', '--with-opt'] assert ac.build()['options']['env'] == {'OPT': 'VAL'}
def test_make(): class AnodMake(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): m1 = Make(self, makefile="/tmp/makefile") m1.set_var("prefix", "/foo") m2 = Make(self, exec_dir="/tmp/exec_dir", jobs=2) m2.set_default_target("install") m2.set_var("profiles", ["dev", "prod"]) return ( m1.cmdline()["cmd"], m1()[0], m1(exec_dir="/foo", timeout=2)[1], m2.cmdline()["cmd"], m2.cmdline(["clean", "install"])["cmd"], m2.cmdline("all")["cmd"], m2()[1], ) Anod.sandbox = SandBox(root_dir=os.getcwd()) Anod.sandbox.create_dirs() am = AnodMake(qualifier="", kind="build", jobs=10) AnodDriver(anod_instance=am, store=None).activate(Anod.sandbox, None) am.build_space.create() assert am.build() == ( ["make", "-f", "/tmp/makefile", "-j", "10", "prefix=/foo"], ("make", "-f", "/tmp/makefile", "-j", "10", "prefix=/foo"), {"cwd": "/foo", "timeout": 2}, ["make", "-j", "2", "profiles=dev prod", "install"], ["make", "-j", "2", "profiles=dev prod", "clean", "install"], ["make", "-j", "2", "profiles=dev prod", "all"], {"cwd": "/tmp/exec_dir", "timeout": None}, )
def test_make(): class AnodMake(Anod): def shell(self, *cmd, **kwargs): """Mock for Anod.shell that does not spawn processes.""" return (cmd, kwargs) @Anod.primitive() def build(self): m1 = Make(self, makefile='/tmp/makefile') m1.set_var('prefix', '/foo') m2 = Make(self, jobs=2) m2.set_default_target('install') m2.set_var('profiles', ['dev', 'prod']) return (m1.cmdline()['cmd'], m1()[0], m1(exec_dir='/foo', timeout=2)[1], m2.cmdline()['cmd'], m2.cmdline(['clean', 'install'])['cmd'], m2.cmdline('all')['cmd']) Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.create_dirs() am = AnodMake(qualifier='', kind='build', jobs=10) AnodDriver(anod_instance=am, store=None).activate(Anod.sandbox, None) am.build_space.create() assert am.build() == ( ['make', '-f', '/tmp/makefile', '-j', '10', 'prefix=/foo'], ('make', '-f', '/tmp/makefile', '-j', '10', 'prefix=/foo'), {'cwd': '/foo', 'timeout': 2}, ['make', '-j', '2', 'profiles=dev prod', 'install'], ['make', '-j', '2', 'profiles=dev prod', 'clean', 'install'], ['make', '-j', '2', 'profiles=dev prod', 'all'])
def test_primitive(): class NoPrimitive(Anod): def build(self): return 2 no_primitive = NoPrimitive('', 'build') assert has_primitive(no_primitive, 'build') is False class WithPrimitive(Anod): build_qualifier_format = (('error', False), ) package = Anod.Package(prefix='mypackage', version=lambda: '42') @Anod.primitive() def build(self): if 'error' in self.parsed_qualifier: raise ValueError(self.parsed_qualifier['error']) elif 'error2' in self.parsed_qualifier: self.shell(sys.executable, '-c', 'import sys; sys.exit(2)') else: hello = self.shell(sys.executable, '-c', 'print("world")', output=subprocess.PIPE) return hello.out.strip() with_primitive = WithPrimitive('', 'build') with_primitive2 = WithPrimitive('error=foobar', 'build') with_primitive3 = WithPrimitive('error2', 'build') with_primitive4 = WithPrimitive('error3', 'build') Anod.sandbox = SandBox() Anod.sandbox.root_dir = os.getcwd() Anod.sandbox.spec_dir = os.path.join(os.path.dirname(__file__), 'data') Anod.sandbox.create_dirs() # Activate the logging AnodDriver(anod_instance=with_primitive, store=None).activate() AnodDriver(anod_instance=with_primitive2, store=None).activate() AnodDriver(anod_instance=with_primitive3, store=None).activate() AnodDriver(anod_instance=with_primitive4, store=None) # don't activate with_primitive.build_space.create() assert has_primitive(with_primitive, 'build') is True assert with_primitive.build() == 'world' assert with_primitive.has_nsis is False with_primitive2.build_space.create() with pytest.raises(AnodError) as err: with_primitive2.build() assert 'foobar' in str(err.value) assert with_primitive2.package.name.startswith('mypackage') # Check __getitem__ # PKG_DIR returns the path to the pkg directory assert with_primitive2['PKG_DIR'].endswith('pkg') # Check access to build_space config dict directly in Anod instance with_primitive2.build_space.config['config-key'] = 'config-value' assert with_primitive2['config-key'] == 'config-value' with_primitive3.build_space.create() with pytest.raises(ShellError) as err: with_primitive3.build() assert 'build fails' in str(err.value) with_primitive3.build_space.set_logging() with pytest.raises(ShellError) as err: with_primitive3.build() assert 'build fails' in str(err.value) with open(with_primitive3.build_space.log_file) as f: assert 'import sys; sys.exit(2)' in f.read() with_primitive3.build_space.end() with pytest.raises(AnodError) as err: with_primitive4.build() assert 'AnodDriver.activate() has not been run' in str(err)