Skip to content

Commit a309449

Browse files
committed
fix tests after adding new command, use firmware/cloud env type since the user has more context with those types
1 parent 07dfc55 commit a309449

File tree

4 files changed

+64
-52
lines changed

4 files changed

+64
-52
lines changed

src/cli/usb.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ module.exports = ({ commandProcessor, root }) => {
168168
}
169169
});
170170

171-
commandProcessor.createCommand(usb, 'env', 'Get environment variables from a device', {
171+
commandProcessor.createCommand(usb, 'env', 'Gets environment variables from a device', {
172172
params: '[devices...]',
173173
options: commonOptions,
174174
examples: {
175-
'$0 $command': 'Get environment variables from the connected device',
176-
'$0 $command --all': 'Get environment variables from all devices connected over USB',
177-
'$0 $command my_device': 'Get environment variables from the device named "my_device"'
175+
'$0 $command': 'Gets environment variables from the connected device',
176+
'$0 $command --all': 'Gets environment variables from all devices connected over USB',
177+
'$0 $command my_device': 'Gets environment variables from the device named "my_device"'
178178
},
179179
handler: (args) => {
180180
return usbCommand().getEnv(args);

src/cli/usb.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('USB Command-Line Interface', () => {
4040
' configure Update the system USB configuration',
4141
' cloud-status Check a device\'s cloud connection state',
4242
' network-interfaces Gets the network configuration of the device',
43+
' env Gets environment variables from a device',
4344
''
4445
].join('\n'));
4546
});

src/cmd/usb.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -137,55 +137,59 @@ module.exports = class UsbCommand extends CLICommandBase {
137137

138138
_formatEnvOutput(result, platformName, deviceId) {
139139
const output = [];
140+
const push = line => output.push(line);
140141

141-
output.push('');
142-
output.push(`${chalk.bold('Device:')} ${chalk.cyan(deviceId)} (${chalk.cyan(platformName)})`);
142+
push('');
143+
push(`${chalk.bold('Device:')} ${chalk.cyan(deviceId)} (${chalk.cyan(platformName)})`);
143144

144145
if (result.snapshot) {
145-
output.push(`${chalk.bold('Snapshot Hash:')} ${chalk.gray(result.snapshot.hash)}`);
146+
push(`${chalk.bold('Snapshot Hash:')} ${chalk.gray(result.snapshot.hash)}`);
146147
}
147148

148-
const envVars = result.env;
149-
const envKeys = Object.keys(envVars);
149+
const envVars = result.env ?? {};
150+
const entries = Object.entries(envVars);
150151

151-
if (envKeys.length === 0) {
152-
output.push(chalk.yellow(' No environment variables set'));
153-
} else {
154-
output.push(chalk.bold(`${os.EOL}Environment Variables:`));
155-
const appVars = [];
156-
const systemVars = [];
152+
if (entries.length === 0) {
153+
push(chalk.yellow(' No environment variables set'));
154+
push('');
155+
return output;
156+
}
157157

158-
envKeys.forEach(key => {
159-
const varInfo = envVars[key];
160-
if (varInfo.isApp) {
161-
appVars.push({ key, value: varInfo.value });
162-
} else {
163-
systemVars.push({ key, value: varInfo.value });
164-
}
165-
});
158+
push(chalk.bold(`${os.EOL}Environment Variables:`));
166159

167-
if (appVars.length > 0) {
168-
output.push(chalk.dim(' Application:'));
169-
appVars.sort((a, b) => a.key.localeCompare(b.key)).forEach(({ key, value }) => {
170-
output.push(` ${chalk.green(key)}=${chalk.white(value)}`);
171-
});
172-
}
160+
const { appVars, systemVars } = this._groupEnvVars(entries);
173161

174-
if (systemVars.length > 0) {
175-
if (appVars.length > 0) {
176-
output.push('');
177-
}
178-
output.push(chalk.dim(' System:'));
179-
systemVars.sort((a, b) => a.key.localeCompare(b.key)).forEach(({ key, value }) => {
180-
output.push(` ${chalk.cyan(key)}=${chalk.white(value)}`);
181-
});
182-
}
183-
}
184-
output.push('');
162+
this._renderEnvGroup(output, ' Firmware:', appVars, 'green');
163+
appVars.length > 0 && systemVars.length > 0 && push('');
164+
this._renderEnvGroup(output, ' Cloud:', systemVars, 'cyan');
185165

166+
push('');
186167
return output;
187168
}
188169

170+
_groupEnvVars(entries) {
171+
return entries.reduce(
172+
(acc, [key, { value, isApp }]) => {
173+
const target = isApp ? acc.appVars : acc.systemVars;
174+
target.push({ key, value });
175+
return acc;
176+
},
177+
{ appVars: [], systemVars: [] }
178+
);
179+
}
180+
181+
_renderEnvGroup(output, title, vars, color) {
182+
if (vars.length === 0) {
183+
return;
184+
}
185+
output.push(chalk.dim(title));
186+
vars.sort((a, b) => a.key.localeCompare(b.key))
187+
.forEach(({ key, value }) => {
188+
output.push(` ${chalk[color](key)}=${chalk.white(value)}`);
189+
});
190+
191+
}
192+
189193
stopListening(args) {
190194
args.api = this._api;
191195
args.auth = this._auth;

src/cmd/usb.test.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ describe('USB Commands', () => {
137137
expect(cleanOutput).to.deep.equal([
138138
'',
139139
'Device: 0123456789abcdef (P2)',
140-
'\nEnvironment Variables:',
141-
' Application:',
140+
`${os.EOL}Environment Variables:`,
141+
' Firmware:',
142142
' FOO=bar',
143143
' TEST=baz',
144144
''
@@ -159,8 +159,8 @@ describe('USB Commands', () => {
159159
expect(cleanOutput).to.deep.equal([
160160
'',
161161
'Device: 0123456789abcdef (P2)',
162-
'\nEnvironment Variables:',
163-
' System:',
162+
`${os.EOL}Environment Variables:`,
163+
' Cloud:',
164164
' SYS_VAR1=value1',
165165
' SYS_VAR2=value2',
166166
''
@@ -182,12 +182,12 @@ describe('USB Commands', () => {
182182
expect(cleanOutput).to.deep.equal([
183183
'',
184184
'Device: abc123def456 (Photon)',
185-
'\nEnvironment Variables:',
186-
' Application:',
185+
`${os.EOL}Environment Variables:`,
186+
' Firmware:',
187187
' ANOTHER_APP=another_app',
188188
' APP_KEY=app_value',
189189
'',
190-
' System:',
190+
' Cloud:',
191191
' SYS_KEY=sys_value',
192192
''
193193
]);
@@ -226,8 +226,8 @@ describe('USB Commands', () => {
226226
'',
227227
'Device: 0123456789abcdef (P2)',
228228
'Snapshot Hash: abc123def456789',
229-
'\nEnvironment Variables:',
230-
' Application:',
229+
`${os.EOL}Environment Variables:`,
230+
' Firmware:',
231231
' MY_VAR=my_value',
232232
''
233233
]);
@@ -251,12 +251,12 @@ describe('USB Commands', () => {
251251
'',
252252
'Device: device123 (P2)',
253253
`${os.EOL}Environment Variables:`,
254-
' Application:',
254+
' Firmware:',
255255
' APPLE=a',
256256
' BANANA=b',
257257
' ZEBRA=z',
258258
'',
259-
' System:',
259+
' Cloud:',
260260
' SYS_A=sa',
261261
' SYS_Z=sz',
262262
''
@@ -273,7 +273,14 @@ describe('USB Commands', () => {
273273
const output = usbCommands._formatEnvOutput(result, 'P2', 'device123');
274274
const cleanOutput = output.map(stripAnsi);
275275

276-
expect(cleanOutput[4]).to.equal(' SPECIAL=value with spaces & symbols!@#$%');
276+
expect(cleanOutput).to.deep.equal([
277+
'',
278+
'Device: device123 (P2)',
279+
`${os.EOL}Environment Variables:`,
280+
' Firmware:',
281+
' SPECIAL=value with spaces & symbols!@#$%',
282+
''
283+
]);
277284
});
278285
});
279286
});

0 commit comments

Comments
 (0)