Ejemplo n.º 1
0
    def test_unit_test_with_deps(self):
        app_dir = join(self.test_dir, 'test_app')
        dep_dir = join(app_dir, 'deps')
        set_deps(
            app_dir,  # root project depends on dep1
            [{
                'name': 'dep1',
                'url': 'https://github.com/comtihon/dep1',
                'tag': '1.0.0'
            }])
        create(dep_dir, {'<name>': 'dep1'})  # dep1 has dep_api:test/0
        with open(join(dep_dir, 'dep1', 'src', 'dep_api.erl'), 'w') as test:
            test.write('''
            -module(dep_api).
            -export([test/0]).
                          
            test() ->
                true.''')
        app_test_dir = join(app_dir, 'test')
        ensure_dir(app_test_dir)
        # dep_api:test/0 is used in unit test of root project
        with open(join(app_test_dir, 'common.erl'), 'w') as test:
            test.write('''
            -module(common).
            -include_lib("eunit/include/eunit.hrl").

            run_test() ->
               ?assertEqual(true, dep_api:test()).''')
        # Compile dep. I only do it in test, as in real life deps will be compiled and linked before running unit.
        dep = Package.from_path(join(dep_dir, 'dep1'))
        self.assertEqual(True, EnotCompiler(dep).compile())
        package = Package.from_path(app_dir)
        compiler = EnotCompiler(package)
        self.assertEqual(True, compiler.unit())
Ejemplo n.º 2
0
 def test_app_creating_duplucates(self):
     ensure_dir(self.src_dir)
     with open(join(self.src_dir, 'test.app.src'), 'w') as w:
         w.write(get_application(['test_dep1', 'test_dep2']))
     with open(join(self.test_dir, 'enot_config.json'), 'w') as w:
         w.write(get_package_conf([{'name': 'test_dep1',
                                    'url': "http://github/comtihon/test_dep1",
                                    'tag': "test_vsn"},
                                   {'name': 'test_dep3',
                                    'url': "http://github/comtihon/test_dep3",
                                    'tag': "test_vsn"}]))
     package = Package.from_path(self.test_dir)
     package_deps = [dep.name for dep in package.deps]
     self.assertEqual(2, len(package_deps))
     self.assertEqual(True, 'test_dep1' in package_deps)
     self.assertEqual(True, 'test_dep3' in package_deps)
     applications = package.app_config.applications
     self.assertEqual(2, len(package_deps))
     self.assertEqual(True, 'test_dep1' in applications)
     self.assertEqual(True, 'test_dep2' in applications)
     apps = package.apps
     self.assertEqual(3, len(apps))  # test_dep1, test_dep2, test_dep3
     self.assertEqual(True, 'test_dep1' in apps)
     self.assertEqual(True, 'test_dep2' in apps)
     self.assertEqual(True, 'test_dep3' in apps)
Ejemplo n.º 3
0
    def test_collecting_units_recurse(self):
        test_dir = join(self.test_dir, 'test_app', 'test')
        ensure_dir(test_dir)
        with open(join(test_dir, 'level1.erl'), 'w') as test:
            test.write('''
            -module(level1).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?_assert(true).''')
        subdir = join(test_dir, 'sub')
        ensure_dir(subdir)
        with open(join(subdir, 'level2.erl'), 'w') as test:
            test.write('''
            -module(level2).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?_assert(true).''')
        package = Package.from_path(join(self.test_dir, 'test_app'))
        compiler = EnotCompiler(package)
        files = compiler._EnotCompiler__get_all_files(compiler.test_path,
                                                      'erl')
        self.assertEqual({
            'level1': test_dir,
            'level2': join(test_dir, 'sub')
        }, files)
Ejemplo n.º 4
0
 def test_link_dep(self, mock_conf, _):
     mock_conf.return_value = self.conf_file
     pack_path = join(self.test_dir, 'test_app')
     set_deps(pack_path, [{
         'name': 'dep',
         'url': 'https://github.com/comtihon/dep',
         'tag': '1.0.0'
     }])
     create(self.tmp_dir, {'<name>': 'dep'})
     dep_tmp_path = join(self.tmp_dir, 'dep')
     set_git_url(dep_tmp_path, 'http://github/comtihon/dep')
     set_git_tag(dep_tmp_path, '1.0.0')
     builder = Builder.init_from_path(pack_path)
     builder.populate()
     self.assertEqual(True, builder.build())
     # dep was added to cache
     self.assertEqual(
         True,
         builder.system_config.cache.exists_local(
             Package.from_path(dep_tmp_path)))
     dep_link_ebin = join(pack_path, 'deps', 'dep', 'ebin')
     self.assertEqual(True, os.path.islink(dep_link_ebin))
     erl = Static.get_erlang_version()
     real_dep = join(self.cache_dir, 'comtihon', 'dep', '1.0.0', erl,
                     'ebin')
     self.assertEqual(real_dep, os.readlink(dep_link_ebin))
Ejemplo n.º 5
0
 def test_app_creating_new_app(self):
     ensure_dir(self.src_dir)
     with open(join(self.src_dir, 'test.app.src'), 'w') as w:
         w.write(get_application(['mnesia']))
     with open(join(self.test_dir, 'enot_config.json'), 'w') as w:
         w.write(get_package_conf([]))
     package = Package.from_path(self.test_dir)
     self.assertEqual([], package.deps)  # no package deps
     self.assertEqual(['mnesia'], package.apps)  # mnesia in applications to be inserted in app (from apps)
     self.assertEqual(['mnesia'], package.app_config.applications)  # mnesia in package apps (from app.src conf)
Ejemplo n.º 6
0
 def test_init_from_path(self):
     create(self.test_dir, {'<name>': 'test_app'})
     set_git_url(join(self.test_dir, 'test_app'), 'http://github/my_namespace/my_project')
     set_git_tag(join(self.test_dir, 'test_app'), '1.0.0')
     pack = Package.from_path(join(self.test_dir, 'test_app'))
     self.assertEqual('test_app', pack.name)
     self.assertEqual('0.0.1', pack.vsn)
     self.assertEqual('http://github/my_namespace/my_project', pack.url)
     self.assertEqual('1.0.0', pack.git_tag)
     self.assertEqual('master', pack.git_branch)
     self.assertEqual([], pack.deps)
Ejemplo n.º 7
0
 def test_package_exists(self, mock_conf):
     mock_conf.return_value = self.conf_file
     pack_path = join(self.test_dir, 'test_app')
     set_git_url(pack_path, 'http://github/comtihon/test_app')
     set_git_tag(pack_path, '1.0.0')
     pack = Package.from_path(pack_path)
     builder = Builder.init_from_path(pack_path)
     self.assertEqual(False, builder.system_config.cache.exists_local(pack))
     self.assertEqual(True, builder.build())
     builder.system_config.cache.add_package_local(pack)
     self.assertEqual(True, builder.system_config.cache.exists_local(pack))
Ejemplo n.º 8
0
    def test_unit_test_fail(self):
        test_dir = join(self.test_dir, 'test_app', 'test')
        ensure_dir(test_dir)
        with open(join(test_dir, 'simple.erl'), 'w') as test:
            test.write('''
            -module(simple).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?assertEqual(true, false).''')
        package = Package.from_path(join(self.test_dir, 'test_app'))
        compiler = EnotCompiler(package)
        self.assertEqual(False, compiler.unit())
Ejemplo n.º 9
0
 def test_app_creating_new_dep(self):
     ensure_dir(self.src_dir)
     with open(join(self.src_dir, 'test.app.src'), 'w') as w:
         w.write(get_application([]))
     with open(join(self.test_dir, 'enot_config.json'), 'w') as w:
         w.write(get_package_conf([{'name': 'test_dep',
                                    'url': "http://github/comtihon/test_dep",
                                    'tag': "test_vsn"}]))
     package = Package.from_path(self.test_dir)
     # test_dep in package deps (from package conf)
     self.assertEqual(['test_dep'], [dep.name for dep in package.deps])
     self.assertEqual(['test_dep'], package.apps)  # test_dep in applications to be inserted in app (from deps)
     self.assertEqual([], package.app_config.applications)  # no applications in app.src
Ejemplo n.º 10
0
 def test_disable_prebuild(self, mock_conf):
     mock_conf.return_value = self.conf_file
     create(self.tmp_dir, {'<name>': 'project'})
     project_dir = join(self.tmp_dir, 'project')
     test_file_path = join(project_dir, 'test_file')
     set_prebuild(project_dir, [{
         'shell': 'echo "test" > ' + test_file_path
     }],
                  disable_prebuild=True)
     self.assertEqual(False, os.path.exists(test_file_path))
     package = Package.from_path(project_dir)
     compiler = EnotCompiler(package)
     self.assertEqual(True, compiler.compile())
     self.assertEqual(False, os.path.exists(test_file_path))
Ejemplo n.º 11
0
 def test_build_parse_transform_first(self, mock_conf):
     mock_conf.return_value = self.conf_file
     create(self.tmp_dir, {'<name>': 'project'})
     project_dir = join(self.tmp_dir, 'project')
     project_src = join(project_dir, 'src')
     with open(join(project_src, 'p_trans.erl'), 'w') as w:
         w.write('''
         -module(p_trans).
         -export([parse_transform/2]).
         
         -record(support, {add_fun = true, 
                           export = true}).
         
         parse_transform(AST, _Options) ->
           do_parse([], AST, #support{}).
           
         do_parse(AST, [], _) -> lists:reverse(AST);
         do_parse(AST, [F = {function, N, _, _, _} | Others], Support = #support{add_fun = true}) ->
           M = N - 1,
           AddedFun =
             {function, M, sum, 2,
               [{clause, M,
                 [{var, M, 'A'}, {var, M, 'B'}],
                 [],
                 [{op, M, '+', {var, M, 'A'}, {var, M, 'B'}}]}]},
           TurnedOff = Support#support{add_fun = false},
           do_parse([F | [AddedFun | AST]], Others, TurnedOff);
         do_parse(AST, [E = {attribute, N, export, _} | Others], Support = #support{export = true}) ->
           Export = [E | AST],
           Exported = {attribute, N + 1, export, [{sum, 2}]},
           TurnedOff = Support#support{export = false},
           do_parse([Exported | Export], Others, TurnedOff);
         do_parse(AST, [H | Others], Support) ->
           do_parse([H | AST], Others, Support).
         ''')
     with open(join(project_src, 'a_module.erl'), 'w') as w:
         w.write('''
         -module(a_module).
         
         -compile([{parse_transform, p_trans}]).
         
         -export([hello/0]).
         
         hello() -> hello.
         ''')
     package = Package.from_path(project_dir)
     compiler = EnotCompiler(package)
     self.assertEqual(True, compiler.compile())
     self.assertEqual(True, os.path.exists(join(project_dir, 'ebin')))
Ejemplo n.º 12
0
    def test_proper_project_compilation(self):
        create(self.test_dir, {'<name>': 'proper'})
        project_dir = join(self.test_dir, 'proper')
        ensure_dir(join(project_dir, 'c_src'))
        with open(join(project_dir, 'c_src/proper.c'), 'w') as w:
            w.write('''
            #include "erl_nif.h"

            static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
            {
                return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1);
            }
            
            static ErlNifFunc nif_funcs[] =
            {
                {"hello", 0, hello}
            };
            
            ERL_NIF_INIT(proper,nif_funcs,NULL,NULL,NULL,NULL)
            ''')
        with open(join(project_dir, 'src/proper.erl'), 'w') as w:
            w.write('''
            -module(proper).

            -export([init/0, hello/0]).
            
            init() ->
                  erlang:load_nif("priv/proper", 0),
                  io:format("~p~n", [hello()]).
            
            hello() ->
                  "NIF library not loaded".
            ''')
        package = Package.from_path(project_dir)
        compiler = EnotCompiler(package)
        self.assertEqual(True, compiler.compile())
        self.assertEqual(True,
                         os.path.exists(join(project_dir, 'priv/proper.so')))
        self.assertEqual(True,
                         os.path.exists(join(project_dir, 'ebin/proper.beam')))
        p = subprocess.Popen([
            'erl', '-pa', 'ebin', '-run', 'proper', 'init', '-run', 'init',
            'stop', '-noshell'
        ],
                             stdout=PIPE,
                             cwd=project_dir)
        self.assertEqual(0, p.wait(5000))
        self.assertEqual('"Hello world!"\n', p.stdout.read().decode('utf8'))
Ejemplo n.º 13
0
 def uninstall(self, fullname: str) -> bool:
     packages = self.__search_by_name(fullname)
     if not packages:
         warning(fullname + ' not installed')
         return False
     for package in packages:
         vsn = package['vsn']
         erlang_vsns = self.local_cache.get_erl_versions(fullname, package['vsn'])
         [latest_erl] = erlang_vsns[-1:]
         pack = Package.from_path(join(self.local_cache.path, fullname, vsn, latest_erl))
         if not pack.uninstall():
             warning('Can\'t uninstall package ' + fullname + ': ' + vsn)
             return False
         info(fullname + ': ' + vsn + ' uninstalled')
         self.__remove_from_installed(fullname, vsn)
     return True
Ejemplo n.º 14
0
 def test_app_creating_new_app_and_dep(self):
     ensure_dir(self.src_dir)
     with open(join(self.src_dir, 'test.app.src'), 'w') as w:
         w.write(get_application(['mnesia']))
     with open(join(self.test_dir, 'enot_config.json'), 'w') as w:
         w.write(get_package_conf([{'name': 'test_dep',
                                    'url': "http://github/comtihon/test_dep",
                                    'tag': "test_vsn"}]))
     package = Package.from_path(self.test_dir)
     # test_dep in package deps (from package conf)
     self.assertEqual(['test_dep'], [dep.name for dep in package.deps])
     self.assertEqual(['mnesia'], package.app_config.applications)  # mnesia in package apps (from app.src conf)
     apps = package.apps
     self.assertEqual(2, len(apps))  # mnesia and test_dep will go to app file
     self.assertEqual(True, 'test_dep' in apps)
     self.assertEqual(True, 'mnesia' in apps)
Ejemplo n.º 15
0
 def test_common_test_fail(self):
     test_dir = join(self.test_dir, 'test_app', 'test')
     ensure_dir(test_dir)
     with open(join(test_dir, 'common_SUITE.erl'), 'w') as test:
         test.write('''
         -module(common_SUITE).
         -include_lib("common_test/include/ct.hrl").
         -export([all/0]).
         -export([test/1]).
          
         all() -> [test].
          
         test(_Config) ->
             1 = 2.''')
     package = Package.from_path(join(self.test_dir, 'test_app'))
     compiler = EnotCompiler(package)
     self.assertEqual(False, compiler.common('test/logs'))
Ejemplo n.º 16
0
 def test_write_app_file_from_src(self):
     ensure_dir(self.src_dir)
     with open(join(self.src_dir, 'proper.erl'), 'w') as w:
         w.write('''
         -module(proper).
         -export([test/0]).
         test() -> do_smth(1).
         do_smth(A) -> A + 1.
         ''')
     with open(join(self.src_dir, 'proper.app.src'), 'w') as w:
         w.write('''
         {application, proper,
           [
             {description, ""},
             {vsn, "{{ app.vsn }}"},
             {registered, []},
             {modules, {{ modules }}},
             {applications, {{ app.std_apps + app.apps }}},
             {mod, {proper_app, []}},
             {env, []}
           ]}.
         ''')
     with open(join(self.test_dir, 'enot_config.json'), 'w') as w:
         w.write('''{
         \"name\":\"proper\",
         \"app_vsn\":\"1.0.0\",
         \"deps\": [{\"name\": \"test_dep\",
                     \"url\": \"http://github/comtihon/test_dep\",
                     \"tag\": \"test_vsn\"}]
         }''')
     package = Package.from_path(self.test_dir)
     compiler = EnotCompiler(package)
     self.assertEqual(True, compiler.compile())
     self.assertEqual(True, os.path.exists(self.ebin_dir))
     ls = listdir(self.ebin_dir)
     self.assertEqual(True, 'proper.beam' in ls)
     self.assertEqual(True, 'proper.app' in ls)
     self.assertEqual(2, len(ls))
     (name, vsn, deps, _) = parse_app_config(self.ebin_dir, '.app')
     self.assertEqual('proper', name)
     self.assertEqual('1.0.0', vsn)
     self.assertEqual(deps, ['kernel', 'stdlib', 'test_dep'])
Ejemplo n.º 17
0
    def test_error_project_compilation(self):
        create(self.test_dir, {'<name>': 'proper'})
        project_dir = join(self.test_dir, 'proper')
        ensure_dir(join(project_dir, 'c_src'))
        with open(join(project_dir, 'c_src/proper.c'), 'w') as w:
            w.write('''
            #include "erl_nif.h"

            static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
            {
                return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1)  // error here
            }
            
            static ErlNifFunc nif_funcs[] =
            {
                {"hello", 0, hello}
            };
            
            ERL_NIF_INIT(proper,nif_funcs,NULL,NULL,NULL,NULL)
            ''')
        with open(join(project_dir, 'src/proper.erl'), 'w') as w:
            w.write('''
            -module(proper).

            -export([init/0, hello/0]).
            
            init() ->
                  erlang:load_nif("priv/proper", 0),
                  io:format("~p~n", [hello()]).
            
            hello() ->
                  "NIF library not loaded".
            ''')
        package = Package.from_path(project_dir)
        compiler = EnotCompiler(package)
        self.assertEqual(False, compiler.compile())
        self.assertEqual(False,
                         os.path.exists(join(project_dir, 'priv/proper.so')))
        self.assertEqual(False,
                         os.path.exists(join(project_dir, 'ebin/proper.beam')))
Ejemplo n.º 18
0
    def test_unit_multiple_test_ok(self, ):
        test_dir = join(self.test_dir, 'test_app', 'test')
        ensure_dir(test_dir)
        with open(join(test_dir, 'simple.erl'), 'w') as test:
            test.write('''
            -module(simple).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?_assert(true).''')
        subdir = join(test_dir, 'sub')
        ensure_dir(subdir)
        with open(join(subdir, 'level2.erl'), 'w') as test:
            test.write('''
            -module(level2).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?_assert(true).''')
        package = Package.from_path(join(self.test_dir, 'test_app'))
        compiler = EnotCompiler(package)
        self.assertEqual(True, compiler.unit())
Ejemplo n.º 19
0
    def test_collecting_units(self):
        test_dir = join(self.test_dir, 'test_app', 'test')
        ensure_dir(test_dir)
        with open(join(test_dir, 'first.erl'), 'w') as test:
            test.write('''
            -module(first).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?_assert(true).''')
        with open(join(test_dir, 'second.erl'), 'w') as test:
            test.write('''
            -module(second).
            -include_lib("eunit/include/eunit.hrl").

           run_test() ->
               ?_assert(true).''')
        package = Package.from_path(join(self.test_dir, 'test_app'))
        compiler = EnotCompiler(package)
        files = compiler._EnotCompiler__get_all_files(compiler.test_path,
                                                      'erl')
        self.assertEqual({'first': test_dir, 'second': test_dir}, files)
Ejemplo n.º 20
0
 def test_add_with_deps(self, mock_conf, _):
     mock_conf.return_value = self.conf_file
     # Create test_app with deps: A and B
     pack_path = join(self.test_dir, 'test_app')
     set_deps(pack_path,
              [
                  {'name': 'a_with_dep_a2',
                   'url': 'https://github.com/comtihon/a_with_dep_a2',
                   'tag': '1.0.0'},
                  {'name': 'b_with_no_deps',
                   'url': 'https://github.com/comtihon/b_with_no_deps',
                   'tag': '1.0.0'}
              ])
     # Create dep A with dep A2 (in tmp, as if we download them from git)
     create(self.tmp_dir, {'<name>': 'a_with_dep_a2'})
     dep_a1_path = join(self.tmp_dir, 'a_with_dep_a2')
     set_deps(dep_a1_path, [{'name': 'a2_with_no_deps',
                             'url': 'https://github.com/comtihon/a2_with_no_deps',
                             'tag': '1.0.0'}])
     set_git_url(dep_a1_path, 'http://github/comtihon/a2_with_no_deps')
     set_git_tag(dep_a1_path, '1.0.0')
     # Create dep B (in tmp, as if we download them from git)
     create(self.tmp_dir, {'<name>': 'b_with_no_deps'})
     dep_b_path = join(self.tmp_dir, 'b_with_no_deps')
     set_git_url(dep_b_path, 'http://github/comtihon/b_with_no_deps')
     set_git_tag(dep_b_path, '1.0.0')
     # Create dep A2 (in tmp, as if we download them from git)
     create(self.tmp_dir, {'<name>': 'a2_with_no_deps'})
     dep_a2_path = join(self.tmp_dir, 'a2_with_no_deps')
     set_git_url(dep_a2_path, 'http://github/comtihon/a2_with_no_deps')
     set_git_tag(dep_a2_path, '1.0.0')
     # Compile test_project
     builder = Builder.init_from_path(pack_path)
     self.assertEqual(False, builder.system_config.cache.exists_local(Package.from_path(dep_a1_path)))
     self.assertEqual(False, builder.system_config.cache.exists_local(Package.from_path(dep_b_path)))
     self.assertEqual(False, builder.system_config.cache.exists_local(Package.from_path(dep_a2_path)))
     builder.populate()
     self.assertEqual(True, builder.build())
     self.assertEqual(True, builder.system_config.cache.exists_local(Package.from_path(dep_a1_path)))
     self.assertEqual(True, builder.system_config.cache.exists_local(Package.from_path(dep_b_path)))
     self.assertEqual(True, builder.system_config.cache.exists_local(Package.from_path(dep_a2_path)))
Ejemplo n.º 21
0
 def init_from_path(cls, path) -> 'Builder':
     package = Package.from_path(path)
     return cls(path, package)