aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Roman <george.roman.99@gmail.com>2018-07-04 00:51:50 +0300
committerHisham Muhammad <hisham@gobolinux.org>2018-07-05 15:51:35 -0300
commitcfabbfb04ed77647f68689acb0457c8f9e555ad5 (patch)
tree2a942d7d2bd6368a3698dcdfd4b68ea5de466e78
parent67a8c5c13f057163273c15dea73491a86f61c676 (diff)
downloadluarocks-cfabbfb04ed77647f68689acb0457c8f9e555ad5.tar.gz
luarocks-cfabbfb04ed77647f68689acb0457c8f9e555ad5.tar.bz2
luarocks-cfabbfb04ed77647f68689acb0457c8f9e555ad5.zip
Tests: unit tests for build.builtin
-rw-r--r--spec/build_spec.lua473
1 files changed, 454 insertions, 19 deletions
diff --git a/spec/build_spec.lua b/spec/build_spec.lua
index c7fd2a22..03f60d18 100644
--- a/spec/build_spec.lua
+++ b/spec/build_spec.lua
@@ -30,6 +30,18 @@ local extra_rocks = {
30 "/validate-args-1.5.4-1.rockspec" 30 "/validate-args-1.5.4-1.rockspec"
31} 31}
32 32
33local c_module_source = [[
34 #include <lua.h>
35 #include <lauxlib.h>
36
37 int luaopen_c_module(lua_State* L) {
38 lua_newtable(L);
39 lua_pushinteger(L, 1);
40 lua_setfield(L, -2, "c_module");
41 return 1;
42 }
43]]
44
33describe("LuaRocks build tests #integration", function() 45describe("LuaRocks build tests #integration", function()
34 46
35 before_each(function() 47 before_each(function()
@@ -60,24 +72,10 @@ describe("LuaRocks build tests #integration", function()
60 } 72 }
61 } 73 }
62 ]], finally) 74 ]], finally)
63 write_file("c_module.c", [[ 75 write_file("c_module.c", c_module_source, finally)
64 #include <lua.h>
65 #include <lauxlib.h>
66
67 int luaopen_c_module(lua_State* L) {
68 lua_newtable(L);
69 lua_pushinteger(L, 1);
70 lua_setfield(L, -2, "c_module");
71 return 1;
72 }
73 ]], finally)
74 76
75 assert.is_true(run.luarocks_bool("build")) 77 assert.is_true(run.luarocks_bool("build"))
76 if test_env.TEST_TARGET_OS == "windows" then 78 assert.truthy(lfs.attributes(tmpdir .. "/c_module." .. test_env.lib_extension))
77 assert.truthy(lfs.attributes(tmpdir .. "/c_module.dll"))
78 else
79 assert.truthy(lfs.attributes(tmpdir .. "/c_module.so"))
80 end
81 79
82 lfs.chdir(olddir) 80 lfs.chdir(olddir)
83 lfs.rmdir(tmpdir) 81 lfs.rmdir(tmpdir)
@@ -284,9 +282,7 @@ describe("LuaRocks build tests #integration", function()
284 282
285 lfs.mkdir("autodetect") 283 lfs.mkdir("autodetect")
286 write_file("autodetect/bla.lua", "return {}", finally) 284 write_file("autodetect/bla.lua", "return {}", finally)
287 write_file("c_module.c", [[ 285 write_file("c_module.c", c_module_source, finally)
288
289 ]], finally)
290 end) 286 end)
291 287
292 after_each(function() 288 after_each(function()
@@ -454,3 +450,442 @@ describe("LuaRocks build tests #integration", function()
454 end) 450 end)
455 451
456end) 452end)
453
454test_env.unload_luarocks()
455test_env.setup_specs()
456local cfg = require("luarocks.core.cfg")
457local deps = require("luarocks.deps")
458local fs = require("luarocks.fs")
459local path = require("luarocks.path")
460local rockspecs = require("luarocks.rockspecs")
461local build_builtin = require("luarocks.build.builtin")
462
463describe("LuaRocks build tests #unit", function()
464 local runner
465
466 setup(function()
467 runner = require("luacov.runner")
468 runner.init(testing_paths.testrun_dir .. "/luacov.config")
469 runner.tick = true
470 cfg.init()
471 fs.init()
472 deps.check_lua(cfg.variables)
473 end)
474
475 teardown(function()
476 runner.shutdown()
477 end)
478
479 describe("build.builtin", function()
480 describe("builtin.autodetect_external_dependencies", function()
481 it("returns false if the given build table has no external dependencies", function()
482 local build_table = {
483 type = "builtin"
484 }
485
486 assert.falsy(build_builtin.autodetect_external_dependencies(build_table))
487 end)
488
489 it("returns a table of the external dependencies found in the given build table", function()
490 local build_table = {
491 type = "builtin",
492 modules = {
493 module1 = {
494 libraries = { "foo1", "foo2" },
495 },
496 module2 = {
497 libraries = "foo3"
498 },
499 }
500 }
501
502 local extdeps = build_builtin.autodetect_external_dependencies(build_table)
503 assert.same(extdeps["FOO1"], { library = "foo1" })
504 assert.same(extdeps["FOO2"], { library = "foo2" })
505 assert.same(extdeps["FOO3"], { library = "foo3" })
506 end)
507
508 it("adds proper include and library dirs to the given build table", function()
509 local build_table
510
511 build_table = {
512 type = "builtin",
513 modules = {
514 module1 = {
515 libraries = "foo"
516 }
517 }
518 }
519 build_builtin.autodetect_external_dependencies(build_table)
520 assert.same(build_table, {
521 type = "builtin",
522 modules = {
523 module1 = {
524 libraries = "foo",
525 incdirs = { "$(FOO_INCDIR)" },
526 libdirs = { "$(FOO_LIBDIR)" }
527 }
528 }
529 })
530
531 build_table = {
532 type = "builtin",
533 modules = {
534 module1 = {
535 libraries = "foo",
536 incdirs = { "INCDIRS" }
537 }
538 }
539 }
540 build_builtin.autodetect_external_dependencies(build_table)
541 assert.same(build_table, {
542 type = "builtin",
543 modules = {
544 module1 = {
545 libraries = "foo",
546 incdirs = { "INCDIRS" },
547 libdirs = { "$(FOO_LIBDIR)" }
548 }
549 }
550 })
551
552 build_table = {
553 type = "builtin",
554 modules = {
555 module1 = {
556 libraries = "foo",
557 libdirs = { "LIBDIRS" }
558 }
559 }
560 }
561 build_builtin.autodetect_external_dependencies(build_table)
562 assert.same(build_table, {
563 type = "builtin",
564 modules = {
565 module1 = {
566 libraries = "foo",
567 incdirs = { "$(FOO_INCDIR)" },
568 libdirs = { "LIBDIRS" }
569 }
570 }
571 })
572
573 build_table = {
574 type = "builtin",
575 modules = {
576 module1 = {
577 libraries = "foo",
578 incdirs = { "INCDIRS" },
579 libdirs = { "LIBDIRS" }
580 }
581 }
582 }
583 build_builtin.autodetect_external_dependencies(build_table)
584 assert.same(build_table, {
585 type = "builtin",
586 modules = {
587 module1 = {
588 libraries = "foo",
589 incdirs = { "INCDIRS" },
590 libdirs = { "LIBDIRS" }
591 }
592 }
593 })
594 end)
595 end)
596
597 describe("builtin.autodetect_modules", function()
598 local tmpdir
599 local olddir
600
601 before_each(function()
602 tmpdir = get_tmp_path()
603 olddir = lfs.currentdir()
604 lfs.mkdir(tmpdir)
605 lfs.chdir(tmpdir)
606 fs.change_dir(tmpdir)
607 end)
608
609 after_each(function()
610 if olddir then
611 lfs.chdir(olddir)
612 if tmpdir then
613 lfs.rmdir(tmpdir)
614 end
615 end
616 end)
617
618 local libs = { "foo1", "foo2" }
619 local incdirs = { "$(FOO1_INCDIR)", "$(FOO2_INCDIR)" }
620 local libdirs = { "$(FOO1_LIBDIR)", "$(FOO2_LIBDIR)" }
621
622 it("returns a table of the modules having as location the current directory", function()
623 write_file("module1.lua", "", finally)
624 write_file("module2.c", "", finally)
625 write_file("module3.c", "int luaopen_my_module()", finally)
626 write_file("test.lua", "", finally)
627 write_file("tests.lua", "", finally)
628
629 local modules = build_builtin.autodetect_modules(libs, incdirs, libdirs)
630 assert.same(modules, {
631 module1 = "module1.lua",
632 module2 = {
633 sources = "module2.c",
634 libraries = libs,
635 incdirs = incdirs,
636 libdirs = libdirs
637 },
638 my_module = {
639 sources = "module3.c",
640 libraries = libs,
641 incdirs = incdirs,
642 libdirs = libdirs
643 }
644 })
645 end)
646
647 local test_with_location = function(location)
648 lfs.mkdir(location)
649 lfs.mkdir(location .. "/dir1")
650 lfs.mkdir(location .. "/dir1/dir2")
651
652 write_file(location .. "/module1.lua", "", finally)
653 write_file(location .. "/dir1/module2.c", "", finally)
654 write_file(location .. "/dir1/dir2/module3.c", "int luaopen_my_module()", finally)
655 write_file(location .. "/test.lua", "", finally)
656 write_file(location .. "/tests.lua", "", finally)
657
658 local modules = build_builtin.autodetect_modules(libs, incdirs, libdirs)
659 assert.same(modules, {
660 module1 = location .. "/module1.lua",
661 ["dir1.module2"] = {
662 sources = location .. "/dir1/module2.c",
663 libraries = libs,
664 incdirs = incdirs,
665 libdirs = libdirs
666 },
667 my_module = {
668 sources = location .. "/dir1/dir2/module3.c",
669 libraries = libs,
670 incdirs = incdirs,
671 libdirs = libdirs
672 }
673 })
674
675 lfs.rmdir(location .. "/dir1/dir2")
676 lfs.rmdir(location .. "/dir1")
677 lfs.rmdir(location)
678 end
679
680 it("returns a table of the modules having as location the src directory", function()
681 test_with_location("src")
682 end)
683
684 it("returns a table of the modules having as location the lua directory", function()
685 test_with_location("lua")
686 end)
687
688 it("returns as second and third argument tables of the bin files and copy directories", function()
689 lfs.mkdir("doc")
690 lfs.mkdir("docs")
691 lfs.mkdir("samples")
692 lfs.mkdir("tests")
693 lfs.mkdir("bin")
694 write_file("bin/binfile", "", finally)
695
696 local _, install, copy_directories = build_builtin.autodetect_modules({}, {}, {})
697 assert.same(install, { bin = { "bin/binfile" } })
698 assert.same(copy_directories, { "doc", "docs", "samples", "tests" })
699
700 lfs.rmdir("doc")
701 lfs.rmdir("docs")
702 lfs.rmdir("samples")
703 lfs.rmdir("tests")
704 lfs.rmdir("bin")
705 end)
706 end)
707
708 describe("builtin.run", function()
709 local tmpdir
710 local olddir
711
712 before_each(function()
713 tmpdir = get_tmp_path()
714 olddir = lfs.currentdir()
715 lfs.mkdir(tmpdir)
716 lfs.chdir(tmpdir)
717 fs.change_dir(tmpdir)
718 path.use_tree(lfs.currentdir())
719 end)
720
721 after_each(function()
722 if olddir then
723 lfs.chdir(olddir)
724 if tmpdir then
725 lfs.rmdir(tmpdir)
726 end
727 end
728 end)
729
730 it("returns false if the rockspec has no build modules and its format does not support autoextraction", function()
731 local rockspec = {
732 package = "test",
733 version = "1.0-1",
734 source = {
735 url = "http://example.com/test"
736 },
737 build = {}
738 }
739
740 rockspecs.from_persisted_table("test-1.0-1.rockspec", rockspec)
741 assert.falsy(build_builtin.run(rockspec))
742 rockspec.rockspec_format = "1.0"
743 assert.falsy(build_builtin.run(rockspec))
744 end)
745
746 it("returns false if lua.h could not be found", function()
747 local rockspec = {
748 package = "c_module",
749 version = "1.0-1",
750 source = {
751 url = "http://example.com/c_module"
752 },
753 build = {
754 type = "builtin",
755 modules = {
756 c_module = "c_module.c"
757 }
758 }
759 }
760 write_file("c_module.c", c_module_source, finally)
761
762 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
763 rockspec.variables = { LUA_INCDIR = "invalid" }
764 assert.falsy(build_builtin.run(rockspec))
765 end)
766
767 it("returns false if the build fails", function()
768 local rockspec = {
769 package = "c_module",
770 version = "1.0-1",
771 source = {
772 url = "http://example.com/c_module"
773 },
774 build = {
775 type = "builtin",
776 modules = {
777 c_module = "c_module.c"
778 }
779 }
780 }
781 write_file("c_module.c", c_module_source .. "invalid", finally)
782
783 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
784 assert.falsy(build_builtin.run(rockspec))
785 end)
786
787 it("returns true if the build succeeds with C module", function()
788 local rockspec = {
789 package = "c_module",
790 version = "1.0-1",
791 source = {
792 url = "http://example.com/c_module"
793 },
794 build = {
795 type = "builtin",
796 modules = {
797 c_module = "c_module.c"
798 }
799 }
800 }
801 write_file("c_module.c", c_module_source, finally)
802
803 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
804 assert.truthy(build_builtin.run(rockspec))
805 assert.truthy(lfs.attributes("lib/luarocks/rocks-" .. test_env.lua_version .. "/c_module/1.0-1/lib/c_module." .. test_env.lib_extension))
806 end)
807
808 it("returns true if the build succeeds with Lua module", function()
809 local rockspec = {
810 rockspec_format = "1.0",
811 package = "test",
812 version = "1.0-1",
813 source = {
814 url = "http://example.com/test"
815 },
816 build = {
817 type = "builtin",
818 modules = {
819 test = "test.lua"
820 }
821 }
822 }
823 write_file("test.lua", "return {}", finally)
824
825 rockspecs.from_persisted_table("test-1.0-1.rockspec", rockspec)
826 assert.truthy(build_builtin.run(rockspec))
827 assert.truthy(lfs.attributes("lib/luarocks/rocks-" .. test_env.lua_version .. "/test/1.0-1/lua/test.lua"))
828 end)
829
830 it("automatically extracts the modules and libraries if they are not given and builds against any external dependencies", function()
831 local ssllib = "ssl"
832 if test_env.TEST_TARGET_OS == "windows" then
833 if test_env.MINGW then
834 ssllib = "eay32"
835 else
836 ssllib = "ssleay32"
837 end
838 end
839
840 local rockspec = {
841 rockspec_format = "3.0",
842 package = "c_module",
843 version = "1.0-1",
844 source = {
845 url = "http://example.com/c_module"
846 },
847 external_dependencies = {
848 OPENSSL = {
849 library = ssllib -- Use OpenSSL since it is available on all testing platforms
850 }
851 },
852 build = {
853 type = "builtin"
854 }
855 }
856 write_file("c_module.c", c_module_source, finally)
857
858 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
859 rockspec.variables["OPENSSL_INCDIR"] = test_env.OPENSSL_INCDIR
860 rockspec.variables["OPENSSL_LIBDIR"] = test_env.OPENSSL_LIBDIR
861 assert.truthy(build_builtin.run(rockspec))
862 end)
863
864 it("returns false if any external dependency is missing", function()
865 local rockspec = {
866 rockspec_format = "3.0",
867 package = "c_module",
868 version = "1.0-1",
869 source = {
870 url = "https://example.com/c_module"
871 },
872 external_dependencies = {
873 EXTDEP = {
874 library = "missing"
875 }
876 },
877 build = {
878 type = "builtin"
879 }
880 }
881 write_file("c_module.c", c_module_source, finally)
882
883 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
884 rockspec.variables["EXTDEP_INCDIR"] = lfs.currentdir()
885 rockspec.variables["EXTDEP_LIBDIR"] = lfs.currentdir()
886 assert.falsy(build_builtin.run(rockspec))
887 end)
888 end)
889 end)
890end)
891