Skip to content

Commit

Permalink
Merge pull request #647 from particle-iot/fix/add-proj-prop-for-assets
Browse files Browse the repository at this point in the history
Fix/add project property for assets
  • Loading branch information
keeramis authored Jun 22, 2023
2 parents 07dfa4e + 527423d commit b51320a
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions src/cmd/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const os = require('os');
const _ = require('lodash');
const VError = require('verror');
const prompt = require('inquirer').prompt;
const propertiesParser = require('properties-parser');

const settings = require('../../settings');
const deviceSpecs = require('../lib/device-specs');
Expand Down Expand Up @@ -687,24 +688,22 @@ module.exports = class CloudCommand extends CLICommandBase {
*/
async _checkForAssets(files) {
for (const file of files) {
if (await this._isAssetsDir(file)) {
return path.join(file, 'assets');
}
}
}

async _isAssetsDir(file) {
try {
if ((await fs.stat(file)).isDirectory()) {
const assetsDir = path.join(file, 'assets');
if ((await fs.stat(assetsDir)).isDirectory()) {
return true;
const propPath = path.join(file, 'project.properties');
try {
const savedProp = await fs.readFile(propPath, 'utf8');
const savedPropObj = propertiesParser.parse(savedProp);

if (savedPropObj.assetOtaFolder && savedPropObj.assetOtaFolder !== '') {
const assetsDir = path.join(file, savedPropObj.assetOtaFolder);
const stats = await fs.stat(assetsDir);
if (stats.isDirectory()) {
return assetsDir;
}
}
} catch (error) {
// Ignore file read or stat errors
}
} catch (e) {
// ignore missing files
}
return false;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/cmd/cloud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,36 @@ describe('Cloud Commands', () => {
const dirPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'invalid_no_assets');
expect(await cloud._checkForAssets([dirPath])).to.equal(undefined);
});

it('returns undefined if project.properties is missing', async () => {
const { cloud } = stubForLogin(new CloudCommands(), stubs);
const dirPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'valid-no-proj-prop');
expect(await cloud._checkForAssets([dirPath])).to.equal(undefined);
});

it('returns undefined if project.properties does not have ossetOtaFolder', async () => {
const { cloud } = stubForLogin(new CloudCommands(), stubs);
const dirPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'valid-no-proj-prop');
const projectPropertiesPath = path.join(dirPath, 'project.properties');
const projectPropertiesContent = 'project.name=valid-no-proj-prop\nassetOtaFolder=';
await fs.writeFile(projectPropertiesPath, projectPropertiesContent);

expect(await cloud._checkForAssets([dirPath])).to.equal(undefined);

await fs.unlink(projectPropertiesPath);
});

it('returns undefined if project.properties does not have a valid ossetOtaFolder', async () => {
const { cloud } = stubForLogin(new CloudCommands(), stubs);
const dirPath = path.join(PATH_FIXTURES_THIRDPARTY_OTA_DIR, 'valid-no-proj-prop');
const projectPropertiesPath = path.join(dirPath, 'project.properties');
const projectPropertiesContent = 'project.name=valid-no-proj-prop\nassetOtaFolder=foo';
await fs.writeFile(projectPropertiesPath, projectPropertiesContent);

expect(await cloud._checkForAssets([dirPath])).to.equal(undefined);

await fs.unlink(projectPropertiesPath);
});
});

describe('_getDownloadPathForBin', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
name=stroby
assetOtaFolder=assets
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
meow
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
house
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drip
2 changes: 2 additions & 0 deletions test/__fixtures__/third_party_ota/valid/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=valid
assetOtaFolder=assets
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=zero_assets
assetOtaFolder=assets

0 comments on commit b51320a

Please sign in to comment.