Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML Version as File Modified time #431

Merged
merged 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/sync-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ module.exports = {
recurseFiles,
deleteFilesInFolder: folderPath => recurseFiles(folderPath).forEach(filePath => fs.unlinkSync(filePath)),
readdir: fs.readdirSync,
statSync: fs.statSync,
withoutExtension,
write: (path, data, options = 'utf8') => fs.writeFileSync(path, data, options),
writeBinary: (path, content) => fs.writeFileSync(path, content),
Expand Down
8 changes: 8 additions & 0 deletions src/lib/upload-forms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const argsFormFilter = require('./args-form-filter');
const attachmentsFromDir = require('./attachments-from-dir');
const attachmentFromFile = require('./attachment-from-file');
const crypto = require('crypto');
const fs = require('./sync-fs');
const log = require('./log');
const insertOrReplace = require('./insert-or-replace');
Expand Down Expand Up @@ -38,7 +39,13 @@ module.exports = async (projectDir, subDirectory, options) => {
log.info(`No media directory found at ${mediaDir} for form ${xformPath}`);
}

const hashSum = crypto.createHash('sha256');
const xml = fs.read(xformPath);
hashSum.update(xml);
const xmlVersion = {
time: Date.now(),
sha256: hashSum.digest('hex'),
};

const internalId = readIdFrom(xml);
if(internalId !== baseDocId) log.warn('DEPRECATED', 'Form:', fileName, 'Bad ID set in XML. Expected:', baseDocId, 'but saw:', internalId, ' Support for setting these values differently will be dropped. Please see https://github.com/medic/cht-core/issues/3342.');
Expand All @@ -48,6 +55,7 @@ module.exports = async (projectDir, subDirectory, options) => {
_id: docId,
type: 'form',
internalId: internalId,
xmlVersion: xmlVersion,
title: readTitleFrom(xml),
context: options.default_context,
};
Expand Down
3 changes: 3 additions & 0 deletions test/lib/upload-forms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('upload-forms', () => {
it('should merge supported properties into form', async () => {
sinon.stub(environment, 'isArchiveMode').get(() => false);
sinon.stub(environment, 'pathToProject').get(() => '.');
sinon.stub(Date, 'now').returns(123123);
return uploadForms.__with__({ validateForms })(async () => {
const logInfo = sinon.stub(log, 'info');
const logWarn = sinon.stub(log, 'warn');
Expand All @@ -52,6 +53,8 @@ describe('upload-forms', () => {
const form = await api.db.get('form:example');
expect(form.type).to.equal('form');
expect(form.internalId).to.equal('different');
expect(form.xmlVersion.time).to.equal(123123);
expect(form.xmlVersion.sha256).to.equal('7e3bb121779a8e9f707b6e1db4c1b52aa6e875b5015b41b0a9115efa2d0de1d1');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth leaving a comment in here just to call out that if this assert fails (and you just made changes to the example.xml), then this is expected behavior and you can just update the expected has value....

expect(form.title).to.equal('Merge properties');
expect(form.context).to.deep.equal({ person: true, place: false });
expect(form.icon).to.equal('example');
Expand Down