aboutsummaryrefslogtreecommitdiff
path: root/t/run.lua
blob: a58bb12c02777ec126dae0cddbcd7ae14373fde6 (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
local json = require "libraries.json"
local lpm
local function assert_exists(path) if not io.open(path, "rb")  then error("assertion failed: file " .. path .. " does not exist", 2) end end
local function assert_not_exists(path) if io.open(path, "rb") then error("assertion failed: file " .. path .. " exists", 2) end end
local tmpdir = os.getenv("TMPDIR") or "/tmp"
local fast = os.getenv("FAST")
local userdir = tmpdir .. "/lpmtest"
setmetatable(_G, { __index = function(t, k) if not rawget(t, k) then error("cannot get undefined global variable: " .. k, 2) end end, __newindex = function(t, k) error("cannot set global variable: " .. k, 2) end  })


local tests = {
  ["00_install_singleton"] = function()
    local plugins = lpm("list bracketmatch")["addons"]
    assert(#plugins == 1)
    assert(plugins[1].organization == "singleton")
    assert(plugins[1].status == "available")
    local actions = lpm("install bracketmatch")["actions"]
    assert(actions[1]:find("Installing singleton"))
    assert_exists(userdir .. "/plugins/bracketmatch.lua")
    actions = lpm("uninstall bracketmatch")["actions"]
    assert_not_exists(userdir .. "/plugins/bracketmatch.lua")
  end,
  ["01_upgrade_singleton"] = function()
    lpm("install bracketmatch")
    local plugins = lpm("list bracketmatch")["addons"]
    assert(#plugins == 1)
    assert(plugins[1].status == "installed")
    assert_exists(plugins[1].path)
    io.open(plugins[1].path, "ab"):write("-- this is a test comment to modify the checksum"):close()
    plugins = lpm("list bracketmatch")["addons"]
    assert(#plugins == 2)
    lpm("install bracketmatch")
    plugins = lpm("list bracketmatch")["addons"]
    assert(#plugins == 1)
    lpm("install console")
    assert_exists(userdir .. "/plugins/console/init.lua")
  end,
  ["02_install_complex"] = function()
    local plugins = lpm("list plugin_manager")["addons"]
    assert(#plugins == 1)
    assert(plugins[1].organization == "complex")
    assert(plugins[1].status == "available")
    assert(plugins[1].dependencies.json)
    local actions = lpm("install plugin_manager")["actions"]
    assert_exists(userdir .. "/libraries/json.lua")
    assert_exists(userdir .. "/plugins/plugin_manager")
    assert_exists(userdir .. "/plugins/plugin_manager/init.lua")
    actions = lpm("uninstall plugin_manager")["actions"]
    assert_not_exists(userdir .. "/plugins/plugin_manager")
    lpm("install editorconfig")
    assert_exists(userdir .. "/plugins/editorconfig")
    assert_exists(userdir .. "/plugins/editorconfig/init.lua")
  end,
  ["03_upgrade_complex"] = function()
    local actions = lpm("install plugin_manager")
    local plugins = lpm("list plugin_manager")["addons"]
    assert(#plugins == 1)
    assert(plugins[1].organization == "complex")
    assert(plugins[1].status == "installed")
  end,
  ["04_list_plugins"] = function()
    local plugins = lpm("list")["addons"]
    assert(#plugins > 20)
  end,
  ["05_install_url"] = function()
    local plugins = lpm("list eofnewline")["addons"]
    assert(#plugins == 1)
    assert(plugins[1].organization == "singleton")
    assert(plugins[1].status == "available")
    local actions = lpm("install eofnewline")
    assert_exists(userdir .. "/plugins/eofnewline.lua")
  end,
  ["06_install_stub"] = function()
    local plugins = lpm("list lsp")["addons"]
    assert(#plugins > 1)
    for i, plugin in ipairs(plugins) do
      if plugin.id == "lsp" then
        assert(plugins[1].organization == "complex")
        assert(plugins[1].status == "available")
        local actions = lpm("install lsp")
        assert_exists(userdir .. "/plugins/lsp/init.lua")
        assert_exists(userdir .. "/libraries/widget/init.lua")
        break
      end
    end
    local actions = lpm("install encodings")
    assert_exists(userdir .. "/plugins/encodings.lua")
    local stat = system.stat(userdir .. "/plugins/encodings.lua")
    assert(stat)
    assert(stat.type == "file")
    assert_exists(userdir .. "/libraries/encoding")
    stat = system.stat(userdir .. "/libraries/encoding")
    assert(stat)
    assert(stat.type == "dir")
  end,
  ["07_manifest"] = function()
   local results = json.decode(io.open("manifest.json", "rb"):read("*all"))
   assert(#results["remotes"] == 2)
   assert(#results["addons"] == 3)
  end,
  ["08_install_many"] = function()
    lpm("install encoding gitblame gitstatus language_ts lsp minimap")
  end,
  ["09_misc_commands"] = function()
    lpm("update")
    lpm("upgrade")
  end,
  ["10_install_multiarch"] = function()
    lpm("install plugin_manager --arch x86_64-windows --arch x86_64-linux")
    assert_exists(userdir .. "/plugins/plugin_manager/lpm.x86_64-linux")
    assert_exists(userdir .. "/plugins/plugin_manager/lpm.x86_64-windows.exe")
    assert_exists(userdir .. "/plugins/plugin_manager/init.lua")
  end,
  ["11_dependency_check"] = function()
    lpm("install lsp")
    assert_exists(userdir .. "/plugins/lsp")
    assert_exists(userdir .. "/plugins/lintplus")
    assert_exists(userdir .. "/plugins/settings.lua")
    lpm("uninstall lsp")
    assert_not_exists(userdir .. "/plugins/lsp")
    assert_not_exists(userdir .. "/plugins/lintplus")
    assert_not_exists(userdir .. "/plugins/settings.lua")
  end,
  ["12_masking"] = function()
    lpm("install lsp --mask settings")
    assert_exists(userdir .. "/plugins/lsp")
    assert_exists(userdir .. "/plugins/lintplus")
    assert_not_exists(userdir .. "/plugins/settings.lua")
  end
}

local last_command_result, last_command
lpm = function(cmd)
  last_command = arg[0] .. " --quiet --json --assume-yes --userdir=" .. userdir .. " " .. cmd
  local pipe = io.popen(last_command, "r")
  local result = pipe:read("*all")
  last_command_result = result ~= "" and json.decode(result) or nil
  local success = pipe:close()
  if not success then error("error calling lpm", 2) end
  return last_command_result
end

local function run_tests(tests, arg)
  local fail_count = 0
  local names = {}
  if #arg == 0 then
    for k,v in pairs(tests) do table.insert(names, k) end
  else
    names = arg
  end
  table.sort(names)
  local max_name = 0
  os.execute("rm -rf " .. tmpdir .. "/lpmtest && mkdir -p " .. tmpdir .. "/lpmtest");
  for i,k in ipairs(names) do max_name = math.max(max_name, #k) end
  for i,k in ipairs(names) do
    local v = tests[k]
    if fast then
      os.execute("rm -rf " .. tmpdir .. "/lpmtest/plugins && mkdir -p " .. tmpdir .. "/lpmtest");
    else
      os.execute("rm -rf " .. tmpdir .. "/lpmtest && mkdir -p " .. tmpdir .. "/lpmtest");
    end
    io.stdout:write(string.format("test %-" .. (max_name + 1) .. "s: ", k))
    local failed = false
    xpcall(v, function(err)
      print("[FAIL]: " .. debug.traceback(err, 2))
      if last_command then
        print("Last Command: " .. last_command)
        if last_command_result then
          print(json.encode(last_command_result))
        end
      end
      print()
      print()
      fail_count = fail_count + 1
      failed = true
    end)
    if not failed then
      print("[PASSED]")
    end
  end
  os.exit(fail_count)
end

run_tests(tests, { ... })