aboutsummaryrefslogtreecommitdiff
path: root/spec/init_spec.lua
blob: 88bd23a5568117ac86c3826cb73e2e8a82c62409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
local test_env = require("spec.util.test_env")
local run = test_env.run
local testing_paths = test_env.testing_paths
local copy_dir = test_env.copy_dir
local is_win = test_env.TEST_TARGET_OS == "windows"
local write_file = test_env.write_file
local lfs = require("lfs")

describe("luarocks init #integration", function()

   lazy_setup(function()
      test_env.setup_specs()
   end)

   it("with no arguments", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init"))
         if is_win then
            assert.truthy(lfs.attributes(myproject .. "/lua.bat"))
            assert.truthy(lfs.attributes(myproject .. "/luarocks.bat"))
         else
            assert.truthy(lfs.attributes(myproject .. "/lua"))
            assert.truthy(lfs.attributes(myproject .. "/luarocks"))
         end
         assert.truthy(lfs.attributes(myproject .. "/lua_modules"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua"))
         assert.truthy(lfs.attributes(myproject .. "/.gitignore"))
         assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec"))
      end, finally)
   end)

   it("with --no-gitignore", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init --no-gitignore"))
         if is_win then
            assert.truthy(lfs.attributes(myproject .. "/lua.bat"))
            assert.truthy(lfs.attributes(myproject .. "/luarocks.bat"))
         else
            assert.truthy(lfs.attributes(myproject .. "/lua"))
            assert.truthy(lfs.attributes(myproject .. "/luarocks"))
         end
         assert.truthy(lfs.attributes(myproject .. "/lua_modules"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua"))
         assert.falsy(lfs.attributes(myproject .. "/.gitignore"))
         assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec"))
      end, finally)
   end)

   it("with --no-wrapper-scripts", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init --no-wrapper-scripts"))
         assert.falsy(lfs.attributes(myproject .. "/lua.bat"))
         assert.falsy(lfs.attributes(myproject .. "/luarocks.bat"))
         assert.falsy(lfs.attributes(myproject .. "/lua"))
         assert.falsy(lfs.attributes(myproject .. "/luarocks"))
         assert.truthy(lfs.attributes(myproject .. "/lua_modules"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua"))
         assert.truthy(lfs.attributes(myproject .. "/.gitignore"))
         assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec"))
      end, finally)
   end)

   it("with --wrapper-dir", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init --wrapper-dir=./bin"))
         if is_win then
            assert.truthy(lfs.attributes(myproject .. "/bin/lua.bat"))
            assert.truthy(lfs.attributes(myproject .. "/bin/luarocks.bat"))
         else
            assert.truthy(lfs.attributes(myproject .. "/bin/lua"))
            assert.truthy(lfs.attributes(myproject .. "/bin/luarocks"))
         end
         assert.truthy(lfs.attributes(myproject .. "/lua_modules"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua"))
         assert.truthy(lfs.attributes(myproject .. "/.gitignore"))
         assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec"))
      end, finally)
   end)

   it("lua wrapper works", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init"))
         if is_win then
            assert.truthy(lfs.attributes(myproject .. "/lua.bat"))
            assert.truthy(lfs.attributes(myproject .. "/luarocks.bat"))
            local pd = assert(io.popen([[echo print(_VERSION) | lua.bat]], "r"))
            local output = pd:read("*a")
            pd:close()
            assert.match("5", output, 1, true)
            local fd = io.open("hello.lua", "w")
            fd:write("print('hello' .. _VERSION)")
            fd:close()
            pd = assert(io.popen([[lua.bat hello.lua]], "r"))
            output = pd:read("*a")
            pd:close()
            assert.match("hello", output, 1, true)
         else
            assert.truthy(lfs.attributes(myproject .. "/lua"))
            assert.truthy(lfs.attributes(myproject .. "/luarocks"))
            local pd = assert(io.popen([[echo "print('hello ' .. _VERSION)" | ./lua]], "r"))
            local output = pd:read("*a")
            pd:close()
            assert.match("hello", output, 1, true)
            local fd = io.open("hello.lua", "w")
            fd:write("print('hello' .. _VERSION)")
            fd:close()
            pd = assert(io.popen([[./lua ./hello.lua]], "r"))
            output = pd:read("*a")
            pd:close()
            assert.match("hello", output, 1, true)
         end
      end, finally)
   end)

   it("with given arguments", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init customname 1.0"))
         assert.truthy(lfs.attributes(myproject .. "/customname-1.0-1.rockspec"))
      end, finally)
   end)

   it("with --lua-versions", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init --lua-versions=5.1,5.2,5.3,5.4"))
         local rockspec_name = myproject .. "/myproject-dev-1.rockspec"
         assert.truthy(lfs.attributes(rockspec_name))
         local fd = assert(io.open(rockspec_name, "rb"))
         local data = fd:read("*a")
         fd:close()
         assert.truthy(data:find("lua >= 5.1, < 5.5", 1, true))
      end, finally)
   end)

   it("in a git repo", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         copy_dir(testing_paths.fixtures_dir .. "/git_repo", myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init"))
         local fd = assert(io.open(myproject .. "/myproject-dev-1.rockspec", "r"))
         local content = assert(fd:read("*a"))
         assert.truthy(content:find("summary = \"Test repo\""))
         assert.truthy(content:find("detailed = .+Test repo.+"))
         assert.truthy(content:find("license = \"MIT\""))

         fd = assert(io.open(myproject .. "/.gitignore", "r"))
         content = assert(fd:read("*a"))
         assert.truthy(content:find("/foo"))
         assert.truthy(content:find("/lua"))
         assert.truthy(content:find("/lua_modules"))
      end, finally)
   end)

   it("does not autodetect config or dependencies as modules of the package", function()
      test_env.run_in_tmp(function(tmpdir)
         local myproject = tmpdir .. "/myproject"
         lfs.mkdir(myproject)
         lfs.chdir(myproject)

         assert(run.luarocks("init"))
         assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua"))
         local rockspec_filename = myproject .. "/myproject-dev-1.rockspec"
         assert.truthy(lfs.attributes(rockspec_filename))

         -- install a package locally
         write_file("my_dependency-1.0-1.rockspec", [[
            package = "my_dependency"
            version = "1.0-1"
            source = {
               url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/my_dependency.lua"
            }
            build = {
               type = "builtin",
               modules = {
                  my_dependency = "my_dependency.lua"
               }
            }
         ]], finally)
         write_file(tmpdir .. "/my_dependency.lua", "return {}", finally)

         assert.truthy(run.luarocks("build my_dependency-1.0-1.rockspec"))
         assert.truthy(lfs.attributes(myproject .. "/lua_modules/share/lua/" .. test_env.lua_version .."/my_dependency.lua"))

         os.remove(rockspec_filename)
         os.remove("my_dependency-1.0-1.rockspec")

         -- re-run init
         assert(run.luarocks("init"))

         -- file is recreated
         assert.truthy(lfs.attributes(rockspec_filename))

         local fd = assert(io.open(rockspec_filename, "rb"))
         local rockspec = assert(fd:read("*a"))
         fd:close()

         assert.no.match("my_dependency", rockspec, 1, true)
         assert.no.match("config", rockspec, 1, true)

      end, finally)
   end)
end)