Repository navigation
Expand file tree
/
Copy pathgenerated_sources_classpath_spec.lua
More file actions
166 lines (140 loc) · 6.37 KB
/
Copy pathgenerated_sources_classpath_spec.lua
File metadata and controls
166 lines (140 loc) · 6.37 KB
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
local assert = require('luassert')
describe('Generated sources classpath patcher', function()
local classpath = require('java.experimental.fix-generated-sources')
local path = require('java-core.utils.path')
local temp_dir
local module_root
local classpath_file
local function write_classpath(lines)
vim.fn.writefile(lines, classpath_file)
end
local function read_classpath()
return table.concat(vim.fn.readfile(classpath_file), '\n')
end
before_each(function()
temp_dir = vim.fn.tempname()
module_root = path.join(temp_dir, 'project')
classpath_file = path.join(module_root, '.classpath')
vim.fn.mkdir(path.join(module_root, 'target', 'generated-sources', 'src', 'main', 'java'), 'p')
vim.fn.mkdir(path.join(module_root, 'target', 'generated-sources', 'openapi', 'src', 'main', 'java'), 'p')
vim.fn.mkdir(path.join(module_root, 'target', 'generated-sources', 'foo', 'main', 'java'), 'p')
vim.fn.mkdir(path.join(module_root, 'target', 'generated-sources', 'openapi', 'main', 'java'), 'p')
vim.fn.mkdir(path.join(module_root, 'target', 'generated-sources', 'java'), 'p')
write_classpath({
'<?xml version="1.0" encoding="UTF-8"?>',
'<classpath>',
'\t<classpathentry kind="src" output="target/classes" path="src/main/java">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry kind="src" output="target/classes" path="target/generated-sources">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry kind="output" path="target/classes"/>',
'</classpath>',
})
end)
after_each(function()
vim.fn.delete(temp_dir, 'rf')
end)
it('adds only generated roots that end with src/{segment}/java and excludes them from the parent root', function()
assert.is_true(classpath.patch(temp_dir))
local content = read_classpath()
assert.is_truthy(content:find('excluding="annotations/|openapi/|src/"', 1, true))
assert.is_truthy(content:find('path="target/generated-sources/src/main/java"', 1, true))
assert.is_truthy(content:find('path="target/generated-sources/openapi/src/main/java"', 1, true))
assert.is_falsy(content:find('path="target/generated-sources/foo/main/java"', 1, true))
assert.is_falsy(content:find('path="target/generated-sources/openapi/main/java"', 1, true))
assert.is_falsy(content:find('path="target/generated-sources/java"', 1, true))
end)
it('does not add generated roots when the parent generated-sources entry is missing', function()
write_classpath({
'<?xml version="1.0" encoding="UTF-8"?>',
'<classpath>',
'\t<classpathentry kind="src" output="target/classes" path="src/main/java">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry kind="output" path="target/classes"/>',
'</classpath>',
})
assert.is_false(classpath.patch(temp_dir))
local content = read_classpath()
assert.is_falsy(content:find('target/generated-sources/src/main/java', 1, true))
end)
it('is idempotent — second call returns false and leaves file unchanged', function()
assert.is_true(classpath.patch(temp_dir))
local after_first = read_classpath()
assert.is_false(classpath.patch(temp_dir))
local after_second = read_classpath()
assert.equals(after_first, after_second)
end)
it('preserves pre-existing exclusions in the generated-sources entry', function()
write_classpath({
'<?xml version="1.0" encoding="UTF-8"?>',
'<classpath>',
'\t<classpathentry kind="src" output="target/classes" path="src/main/java">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry excluding="custom/|other/" kind="src" output="target/classes" path="target/generated-sources">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry kind="output" path="target/classes"/>',
'</classpath>',
})
assert.is_true(classpath.patch(temp_dir))
local content = read_classpath()
-- Original exclusions are preserved
assert.is_truthy(content:find('custom/', 1, true))
assert.is_truthy(content:find('other/', 1, true))
-- New exclusions are added
assert.is_truthy(content:find('annotations/', 1, true))
assert.is_truthy(content:find('openapi/', 1, true))
assert.is_truthy(content:find('src/', 1, true))
-- Generated roots are still added
assert.is_truthy(content:find('path="target/generated-sources/src/main/java"', 1, true))
end)
it('handles multi-module projects with multiple .classpath files', function()
local module_b_root = path.join(temp_dir, 'module-b')
local module_b_classpath = path.join(module_b_root, '.classpath')
vim.fn.mkdir(path.join(module_b_root, 'target', 'generated-sources', 'grpc', 'src', 'main', 'java'), 'p')
vim.fn.writefile({
'<?xml version="1.0" encoding="UTF-8"?>',
'<classpath>',
'\t<classpathentry kind="src" output="target/classes" path="src/main/java">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry kind="src" output="target/classes" path="target/generated-sources">',
'\t\t<attributes>',
'\t\t\t<attribute name="optional" value="true"/>',
'\t\t\t<attribute name="maven.pomderived" value="true"/>',
'\t\t</attributes>',
'\t</classpathentry>',
'\t<classpathentry kind="output" path="target/classes"/>',
'</classpath>',
}, module_b_classpath)
assert.is_true(classpath.patch(temp_dir))
-- Module A (original) is patched
local content_a = read_classpath()
assert.is_truthy(content_a:find('path="target/generated-sources/src/main/java"', 1, true))
-- Module B is also patched
local content_b = table.concat(vim.fn.readfile(module_b_classpath), '\n')
assert.is_truthy(content_b:find('path="target/generated-sources/grpc/src/main/java"', 1, true))
end)
end)