From 348db1ebcc32ff66a3974bdf330a5ebb45183551 Mon Sep 17 00:00:00 2001 From: Julien Gilli Date: Wed, 5 Apr 2017 15:19:46 -0700 Subject: [PATCH] make unit not optional in parseVolumeSize --- lib/do_volume/do_create.js | 4 +--- lib/volumes.js | 9 ++++----- test/unit/parseVolumeSize.test.js | 1 - 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/do_volume/do_create.js b/lib/do_volume/do_create.js index 9f48c83..7676670 100644 --- a/lib/do_volume/do_create.js +++ b/lib/do_volume/do_create.js @@ -154,9 +154,7 @@ do_create.options = [ helpArg: 'SIZE', help: 'The size of the volume to create, in the form ' + '``, e.g. `20G`. must be > 0. Supported ' + - 'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.' + - ' If is not provided, is considered to be in ' + - 'mebibytes.', + 'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.', completionType: 'tritonvolumesize' }, { diff --git a/lib/volumes.js b/lib/volumes.js index 4ac8168..d2c27b8 100644 --- a/lib/volumes.js +++ b/lib/volumes.js @@ -20,11 +20,9 @@ function throwInvalidSize(size) { * Returns the number of MiBs (Mebibytes) represented by the string "size". That * string has the following format: . The integer must be > 0. * Unit format suffixes are 'G' or 'g' for gibibytes and 'M' or 'm' for - * mebibytes. If no unit suffix is provided, the unit is considered to be * mebibytes. * * Examples: - * - the string '100' represents 100 mebibytes * - the strings '100m' and '100M' represent 100 mebibytes * - the strings '100g' and '100G' represent 100 gibibytes * @@ -42,11 +40,11 @@ function parseVolumeSize(size) { M: 1 }; - var multiplier = 1; /* default unit is mebibytes */ + var multiplier; var multiplierSymbol; var baseValue; - var matches = size.match(/^([1-9]\d*)(g|m|G|M)?$/); + var matches = size.match(/^([1-9]\d*)(g|m|G|M)$/); if (!matches) { throwInvalidSize(size); } @@ -57,7 +55,8 @@ function parseVolumeSize(size) { } baseValue = Number(matches[1]); - if (isNaN(baseValue)) { + + if (isNaN(baseValue) || multiplier === undefined) { throwInvalidSize(size); } diff --git a/test/unit/parseVolumeSize.test.js b/test/unit/parseVolumeSize.test.js index bab6f06..024aa81 100644 --- a/test/unit/parseVolumeSize.test.js +++ b/test/unit/parseVolumeSize.test.js @@ -63,7 +63,6 @@ test('parseVolumeSize', function (tt) { tt.test('parsing valid sizes', function (t) { var validVolumeSizes = [ - {input: '42', expectedOutput: 42}, {input: '42m', expectedOutput: 42}, {input: '42M', expectedOutput: 42}, {input: '42g', expectedOutput: 42 * 1024},