Skip to content

Commit 5f595d5

Browse files
DrRataplanline-o
authored andcommitted
feat(get): add an option to not expand xincludes
1 parent 1d0a4b3 commit 5f595d5

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

commands/get.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ const xmlBooleanOptionValue = new Map([
5151
const xmlBooleanSetting = {
5252
type: 'string',
5353
coerce: (value) => {
54-
console.log(value)
5554
if (value === null) {
5655
return value
5756
}
5857
if (!xmlBooleanOptionValue.has(value)) {
5958
throw Error('Unsupported XML serialization option value: ' + value)
6059
}
61-
return value
60+
return xmlBooleanOptionValue.get(value)
6261
}
6362
}
64-
const serializationOptionNames = ['insert-final-newline', 'omit-xml-declaration']
63+
const serializationOptionNames = ['insert-final-newline', 'omit-xml-declaration', 'expand-xincludes']
6564

6665
const serializationDefaults = {
66+
'expand-xincludes': 'yes'
6767
// "exist:indent": "no",
6868
// "indent": "no",
6969
// "output.indent": "no",
@@ -214,6 +214,9 @@ async function downloadCollectionOrResource (db, source, target, options) {
214214
console.error('Exclude:\n', ...options.exclude, '\n')
215215
}
216216
console.error(`Downloading up to ${options.threads} resources at a time`)
217+
if (options['expand-xincludes'] === 'false') {
218+
console.error('Skipping XInclude expansion')
219+
}
217220
}
218221

219222
// initial file
@@ -295,6 +298,12 @@ export function builder (yargs) {
295298
default: [],
296299
...stringList
297300
})
301+
.option('x', {
302+
group: 'serialization',
303+
alias: 'expand-xincludes',
304+
describe: 'Skip expanding XInclude elements when set to "false"',
305+
...xmlBooleanSetting
306+
})
298307
.option('X', {
299308
group: 'serialization',
300309
alias: 'omit-xml-declaration',

spec/tests/get.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { test } from 'tape'
22
import { run, asAdmin } from '../test.js'
33
import { readdirSync } from 'node:fs'
4+
import { readFile } from 'node:fs/promises'
5+
import path from 'node:path'
46

57
const testCollectionName = 'get-test'
68
const testCollection = '/db/' + testCollectionName
@@ -34,7 +36,8 @@ const expectedDirectoryListing = [
3436
'empty-subcollection',
3537
'index.html',
3638
'subcollection',
37-
'test.xq'
39+
'test.xq',
40+
'xincludes.xml'
3841
]
3942

4043
async function prepare (t) {
@@ -52,7 +55,14 @@ async function prepare (t) {
5255
storeResourceQuery(testCollection, 'a20.txt', '"test"'),
5356
storeResourceQuery(testCollection, 'a22.xml', '<test />'),
5457
storeResourceQuery(testCollection, 'index.html', '<html><body>1</body></html>'),
55-
storeResourceQuery(testCollection, 'test.xq', '"1"')
58+
storeResourceQuery(testCollection, 'test.xq', '"1"'),
59+
storeResourceQuery(
60+
testCollection,
61+
'xincludes.xml',
62+
`<xml xmlns:xi="http://www.w3.org/2001/XInclude">
63+
<xi:include href="./a22.xml"><xi:fallback><p>I am an x include</p></xi:fallback></xi:include>
64+
</xml>`
65+
)
5666
].join(',')
5767
const { stderr, stdout } = await run('xst', ['run', query], asAdmin)
5868
if (stderr) {
@@ -93,6 +103,43 @@ test('with test collection', async (t) => {
93103
await removeLocalDownload()
94104
})
95105

106+
t.test(`can get ${testCollection} as admin without expanding xincludes`, async (st) => {
107+
const { stderr, stdout } = await run('xst', ['get', testCollection, '.', '--expand-xincludes=no'], asAdmin)
108+
if (stderr) {
109+
return st.fail(stderr)
110+
}
111+
st.plan(4)
112+
113+
st.notOk(stdout, 'no output')
114+
st.deepEqual(readdirSync(testCollectionName), expectedDirectoryListing, 'all files were downloaded')
115+
st.deepEqual(readdirSync(testCollectionName + '/subcollection'), ['b'], 'subcollection contents were downloaded')
116+
117+
const contents = await readFile(path.join(testCollectionName, 'xincludes.xml'), 'utf-8')
118+
st.strictEqual(
119+
contents,
120+
'<xml xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="./a22.xml"><xi:fallback><p>I am an x include</p></xi:fallback></xi:include></xml>'
121+
)
122+
123+
await removeLocalDownload()
124+
})
125+
126+
t.test(`can get ${testCollection} as admin with expanding xincludes`, async (st) => {
127+
const { stderr, stdout } = await run('xst', ['get', testCollection, '.', '--expand-xincludes', 'yes'], asAdmin)
128+
if (stderr) {
129+
return st.fail(stderr)
130+
}
131+
st.plan(4)
132+
133+
st.notOk(stdout, 'no output')
134+
st.deepEqual(readdirSync(testCollectionName), expectedDirectoryListing, 'all files were downloaded')
135+
st.deepEqual(readdirSync(testCollectionName + '/subcollection'), ['b'], 'subcollection contents were downloaded')
136+
137+
const contents = await readFile(path.join(testCollectionName, 'xincludes.xml'), 'utf-8')
138+
st.strictEqual(contents, '<xml xmlns:xi="http://www.w3.org/2001/XInclude"><test/></xml>')
139+
140+
await removeLocalDownload()
141+
})
142+
96143
t.test(`can get ${testCollection} as admin with just one thread`, async (st) => {
97144
const { stderr, stdout } = await run('xst', ['get', testCollection, '.', '--threads', '1'], asAdmin)
98145
if (stderr) {
@@ -141,8 +188,8 @@ test('with test collection', async (t) => {
141188
)
142189
st.equal(
143190
lines.filter((l) => /^ downloaded resource [/\w]+\/get-test/.exec(l)).length,
144-
9,
145-
'notify 9 resources downloaded'
191+
10,
192+
'notify 10 resources downloaded'
146193
)
147194

148195
st.deepEqual(readdirSync(testCollectionName), expectedDirectoryListing, 'all files were downloaded')

0 commit comments

Comments
 (0)