Example #1
0
def test_realize_readonly() -> None:
    with setup_storage2('test_realize_readonly') as (T, S):
        rref1 = realize(instantiate(mkstage, {'a': '1'}, S=S))

        try:
            with open(join(store_rref2path(rref1, S), 'newfile'), 'w') as f:
                f.write('foo')
            raise ShouldHaveFailed('No write-protection??')
        except OSError:
            pass

        try:
            rmtree(store_rref2path(rref1, S))
            raise ShouldHaveFailed('No remove-protection??')
        except OSError:
            pass

        def _realize(b: Build):
            with open(join(build_outpath(b), 'exe'), 'w') as f:
                f.write('#!/bin/sh\necho "Fooo"')
            chmod(join(build_outpath(b), 'exe'), S_IWRITE | S_IREAD | S_IEXEC)

        rref2 = realize(
            instantiate(mkdrv,
                        Config({}),
                        match_some(),
                        build_wrapper(_realize),
                        S=S))
        assert pipe_stdout([join(store_rref2path(rref2,S),'exe')])=='Fooo\n', \
          "Did we lost exec permission?"
Example #2
0
def test_repl_realizeInval():
    with setup_storage('test_repl_realizeInval'):
        try:
            repl_realize(instantiate(mkstage, {'a': 1}),
                         force_interrupt=33)  #type:ignore
            raise ShouldHaveFailed('wrong force_interrupt value')
        except AssertionError:
            pass
Example #3
0
def test_no_dref_deps_without_realizers() -> None:
    with setup_storage2('test_no_dref_deps_without_realizers') as (T, S):
        try:
            clo = instantiate(mkstage, {'a': 1}, S=S)
            _ = realize(instantiate(mkstage, {'maman': clo.dref}, S=S))
            raise ShouldHaveFailed("We shouldn't share DRefs across managers")
        except AssertionError:
            pass
Example #4
0
def test_no_foreign_dref_deps() -> None:
    with setup_storage2('test_no_foreign_dref_deps') as (T, S):

        def _setup(m):
            clo = instantiate(mkstage, {'foo': 'bar'})
            return mkstage(m, {'bogus': clo.dref})

        try:
            dref = instantiate(_setup, S=S)
            raise ShouldHaveFailed(f"Should fail, but got {dref}")
        except AssertionError:
            pass
Example #5
0
def test_no_rref_deps() -> None:
    with setup_storage2('test_no_rref_deps') as (T, S):

        def _setup(m):
            rref = realize(instantiate(mkstage, {'foo': 'bar'}))
            n2 = mkstage(m, {'bogus': rref})
            return n2

        try:
            dref = instantiate(_setup, S=S)
            raise ShouldHaveFailed(f"Should fail, but got {dref}")
        except AssertionError:
            pass
Example #6
0
def test_no_recursive_instantiate_with_same_manager() -> None:
    with setup_storage2('test_no_recursive_instantiate_with_same_manager') as (
            T, S):

        def _setup(m):
            derivs = instantiate_(m, _setup)
            n2 = mkstage(m, {'bogus': derivs.dref})
            return n2

        try:
            dref = instantiate(_setup, S=S)
            raise ShouldHaveFailed(f"Should fail, but got {dref}")
        except AssertionError:
            pass
Example #7
0
def test_bashlike():
  with setup_storage('test_bashlike'):
    clo=instantiate(mkstage, {'a':1}, lambda:42)
    rref1=realize(clo, force_rebuild=[clo.dref])
    rref2=realize(clo, force_rebuild=[clo.dref])
    assert 'artifact' in lsref(rref1)
    assert 'context.json' in lsref(rref1)
    assert '__buildtime__.txt' in lsref(rref1)
    h1,_,_=unrref(rref1)
    h2,_,_=unrref(rref2)
    assert len(lsref(clo.dref))==2
    assert h1 in lsref(clo.dref)
    assert h2 in lsref(clo.dref)
    assert '42' in catref(rref1,['artifact'])
    try:
      lsref('foobar') # type:ignore
      raise ShouldHaveFailed('should reject garbage')
    except AssertionError:
      pass
    try:
      catref(clo.dref, ['artifact']) # type:ignore
      raise ShouldHaveFailed('notimpl')
    except AssertionError:
      pass
Example #8
0
def test_rmdref():
  with setup_storage('test_rmdref') as s:
    clo=instantiate(mkstage, {'a':1}, lambda:42)
    drefpath=store_dref2path(clo.dref)
    rref1=realize(clo, force_rebuild=[clo.dref])
    rrefpath=store_rref2path(rref1)
    assert isdir(rrefpath)
    rmref(rref1)
    assert not isdir(rrefpath)
    assert isdir(drefpath)
    rmref(clo.dref)
    assert not isdir(drefpath)
    try:
      rmref('asdasd') # type:ignore
      raise ShouldHaveFailed('shoud reject garbage')
    except AssertionError:
      pass
Example #9
0
def test_realized() -> None:
    with setup_storage('test_realized'):

        def _setting(m: Manager, assume_realized: bool) -> DRef:
            def _realize(b: Build):
                if assume_realized:
                    raise ShouldHaveFailed('Should not call the real realizer')
                return build_outpath(b)

            return mkdrv(m, mkconfig({'name': '1'}), match_some(),
                         build_wrapper(_realize))

        dref = instantiate(realized(_setting), assume_realized=True)
        try:
            rref = realize(dref)
            raise ShouldHaveFailed('Should not be realized')
        except AssertionError:
            pass

        rref = realize(instantiate(_setting, assume_realized=False))
        rref2 = realize(instantiate(realized(_setting), assume_realized=True))
        assert rref == rref2
Example #10
0
def test_promise() -> None:
    with setup_storage2('test_promise') as (T, S):

        def _setting(m: Manager, fullfill: bool) -> DRef:
            n1 = mkstage(m, {'name': '1', 'promise': [promise, 'artifact']})

            def _realize(b: Build):
                o = build_outpath(b)
                c = build_cattrs(b)
                assert b.dref in c.promise
                assert n1 in store_cattrs(c.maman, S).promise
                assert build_path(b, c.promise) == join(o, 'uber-artifact')
                assert build_path(b,
                                  store_cattrs(c.maman,
                                               S).promise) == build_path(
                                                   b, c.maman_promise)
                if fullfill:
                    with open(build_path(b, c.promise), 'w') as f:
                        f.write('chickenpoop')

            return mkdrv(m,
                         mkconfig({
                             'name': '2',
                             'maman': n1,
                             'promise': [promise, 'uber-artifact'],
                             'maman_promise': [n1, 'artifact']
                         }),
                         matcher=match_some(),
                         realizer=build_wrapper(_realize))

        try:
            rref = realize(instantiate(_setting, False, S=S))
            raise ShouldHaveFailed('Promise trigger')
        except AssertionError:
            pass
        rref = realize(instantiate(_setting, True, S=S))
        assert_valid_rref(rref)
Example #11
0
def test_shellref():
  with setup_storage('test_shellref') as s:
    with TemporaryDirectory() as tmp:
      mockshell=join(tmp,'mockshell')
      with open(mockshell,'w') as f:
        f.write(f"#!/bin/sh\n")
        f.write(f"pwd\n")
      chmod(mockshell, stat(mockshell).st_mode | S_IEXEC)
      environ['SHELL']=mockshell
      rref=realize(instantiate(mkstage, {'a':1}))
      shellref(rref)
      shellref(rref2dref(rref))
      shellref()
      shell(store_rref2path(rref))
      repl_realize(instantiate(mkstage, {'n':1}), force_interrupt=True)
      b=repl_build()
      o=build_outpath(b)
      shell(b)
      repl_cancelBuild(b)
      try:
        shellref('foo') # type:ignore
        raise ShouldHaveFailed('shellref should reject garbage')
      except AssertionError:
        pass
Example #12
0
def test_lens():
  with setup_storage('test_lens'):
    def _setting(m:Manager)->DRef:
      n1=mkstage(m, {'name':'1', 'promise':[promise,'artifact']})
      n2=mkstage(m, {'name':'2', 'promise':[promise,'artifact'],
                        'dict':{'d1':1} })

      def _realize(b:Build):
        o=build_outpath(b)
        c=build_cattrs(b)
        assert isrefpath(mklens(b).maman.promise.refpath)
        assert isfile(mklens(b).papa.promise.syspath)
        assert o in mklens(b).promise.syspath
        assert o == mklens(b).syspath
        assert mklens(b).papa.name.val == '2'
        assert mklens(b).papa.dref == c.papa

        with open(mklens(b).promise.syspath,'w') as f:
          f.write('chickenpoop')

      return mkdrv(m,
        mkconfig({'name':'3', 'maman':n1, 'papa':n2,
                  'promise':[promise,'artifact'],
                  }),
                 matcher=match_some(),
                 realizer=build_wrapper(_realize))

    clo=instantiate(_setting)
    assert isrefpath(mklens(clo.dref).maman.promise.refpath)
    assert isdir(mklens(clo.dref).syspath)
    rref=realize(clo)
    assert_valid_rref(rref)
    assert isrefpath(mklens(rref).maman.promise.refpath)
    assert isfile(mklens(rref).maman.promise.syspath)
    assert mklens(rref).rref == rref
    assert isrefpath(mklens(rref).papa.promise.refpath)
    assert mklens(rref).papa.dict.d1.val == 1
    assert mklens(rref).dref == clo.dref
    assert isdir(mklens(rref).syspath)

    try:
      print(mklens(clo.dref).maman.promise.syspath)
      raise ShouldHaveFailed()
    except AssertionError:
      pass

    try:
      print(mklens(rref).papa.dict.d1.get('xxx'))
      raise ShouldHaveFailed()
    except AssertionError:
      pass

    try:
      print(mklens(rref).papa.dict.d1.syspath)
      raise ShouldHaveFailed()
    except AssertionError:
      pass

    try:
      mklens(rref).papa.dict.d1.val=42 # can't mutate rref
      raise ShouldHaveFailed()
    except AssertionError:
      pass

    d={'foo':'foo','bar':'bar'}
    mklens(d).foo.val='zzz'
    assert d['foo']=='zzz'
    try:
      mklens(d).x.val=42 # can't set new values
      raise ShouldHaveFailed()
    except AssertionError:
      pass
    mklens(d).bar.val+='33'
    assert d['bar']=='bar33'
Example #13
0
def test_config2() -> None:
    try:
        c = mkconfig({'a': (3, 1)})
        raise ShouldHaveFailed(f'RConfig {c} is surprizingly valid')
    except AssertionError:
        pass
Example #14
0
 def _realize(b: Build):
     if assume_realized:
         raise ShouldHaveFailed('Should not call the real realizer')
     return build_outpath(b)