Skip to content

Commit

Permalink
Auto-merge for PR #293 via VersionBot
Browse files Browse the repository at this point in the history
fix(lsblk): Enumerate mountpoints on root devices
  • Loading branch information
resin-io-modules-versionbot[bot] authored Aug 1, 2018
2 parents 24e6485 + bd80d97 commit d75c06e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file
automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
This project adheres to [Semantic Versioning](http://semver.org/).

## v6.3.1 - 2018-08-01

* Test(lsblk): Add test case for root device mountpoints #293 [Jonas Hermsmeier]
* Fix(lsblk): Enumerate mountpoints on root devices #293 [Jonas Hermsmeier]
* Fix(lsblk): Fall back to mountpoint if no label given #293 [Jonas Hermsmeier]
* Fix(lsblk): Resolve relative device names #293 [Jonas Hermsmeier]

## v6.3.0 - 2018-07-09

* Fix(linux): Fix pair consolidation for lsblk #255 [Jonas Hermsmeier]
Expand Down
27 changes: 22 additions & 5 deletions lib/lsblk/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

'use strict';

const path = require('path');

const getMountpoints = (children) => {
return children.filter((child) => {
return child.mountpoint;
Expand All @@ -36,10 +38,10 @@ const getDescription = (device) => {
if (device.children) {
let subLabels = device.children
.filter((c) => {
return c.label && c.label !== device.label;
return c.label && c.label !== device.label || c.mountpoint;
})
.map((c) => {
return c.label;
return c.label || c.mountpoint;
});
subLabels = Array.from(new Set(subLabels));
if (subLabels.length) {
Expand All @@ -49,8 +51,22 @@ const getDescription = (device) => {
return description.join(' ').replace(/\s+/g, ' ').trim();
};

const resolveDeviceName = (name) => {
if (!name) {
return null;
}
if (!path.posix.isAbsolute(name)) {
return path.posix.resolve('/dev', name);
}
return name;
};

const transform = (data) => {
return data.blockdevices.filter((device) => {
return data.blockdevices.map((device) => {
device.name = resolveDeviceName(device.name);
device.kname = resolveDeviceName(device.kname);
return device;
}).filter((device) => {
// Omit loop devices, CD/DVD drives, and RAM
return !device.name.startsWith('/dev/loop')
&& !device.name.startsWith('/dev/sr')
Expand All @@ -66,13 +82,13 @@ const transform = (data) => {
busType: (device.tran || 'UNKNOWN').toUpperCase(),
busVersion: null,
device: device.name,
raw: device.kname,
raw: device.kname || device.name,
description: getDescription(device),
error: null,
size: Number(device.size) || null,
blockSize: Number(device['phy-sec']) || 512,
logicalBlockSize: Number(device['log-sec']) || 512,
mountpoints: device.children ? getMountpoints(device.children) : [],
mountpoints: device.children ? getMountpoints(device.children) : getMountpoints([ device ]),
isReadOnly: isReadOnly,
isSystem: !isRemovable && !isVirtual,
isVirtual: isVirtual,
Expand All @@ -90,3 +106,4 @@ const parse = (stdout) => {
};

module.exports = parse;
module.exports.transform = transform;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drivelist",
"version": "6.3.0",
"version": "6.3.1",
"description": "List all connected drives in your computer, in all major operating systems",
"main": "lib/drivelist.js",
"homepage": "https://github.com/resin-io-modules/drivelist",
Expand Down
44 changes: 44 additions & 0 deletions tests/data/lsblk/no-children-mountpoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"blockdevices": [{
"name": "sda",
"maj:min": "8:0",
"rm": "1",
"size": "14.3G",
"ro": "0",
"type": "disk",
"mountpoint": "/media/jwentz/Temp"
}, {
"name": "nvme0n1",
"maj:min": "259:0",
"rm": "0",
"size": "477G",
"ro": "0",
"type": "disk",
"mountpoint": null,
"children": [{
"name": "nvme0n1p3",
"maj:min": "259:3",
"rm": "0",
"size": "15.8G",
"ro": "0",
"type": "part",
"mountpoint": "[SWAP]"
}, {
"name": "nvme0n1p1",
"maj:min": "259:1",
"rm": "0",
"size": "512M",
"ro": "0",
"type": "part",
"mountpoint": "/boot/efi"
}, {
"name": "nvme0n1p2",
"maj:min": "259:2",
"rm": "0",
"size": "460.7G",
"ro": "0",
"type": "part",
"mountpoint": "/"
}]
}]
}
86 changes: 77 additions & 9 deletions tests/lsblk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@

const fs = require('fs');
const path = require('path');
const util = require('util');
const m = require('mochainon');
const parsePairs = require('../lib/lsblk/pairs.js');
const parseJSON = require('../lib/lsblk/json');
const parsePairs = require('../lib/lsblk/pairs');

const inspect = (value) => {
console.log(util.inspect(value, {
colors: true,
depth: null
}));
};

describe('Drivelist', function() {

Expand Down Expand Up @@ -73,10 +82,7 @@ describe('Drivelist', function() {
isUAS: null
} ];

console.log(require('util').inspect(parsePairs(listData), {
colors: true,
depth: null
}));
inspect(parsePairs(listData));

m.chai.expect(devices).to.deep.equal(expected);

Expand Down Expand Up @@ -138,15 +144,77 @@ describe('Drivelist', function() {
isUAS: null
} ];

console.log(require('util').inspect(parsePairs(listData), {
colors: true,
depth: null
}));
inspect(parsePairs(listData));

m.chai.expect(devices).to.deep.equal(expected);

});

it('can handle mountpoints on root devices', function() {

const listData = require('./data/lsblk/no-children-mountpoints.json');
const actual = parseJSON.transform(listData);

const expected = [ {
enumerator: 'lsblk:json',
busType: 'UNKNOWN',
busVersion: null,
device: '/dev/sda',
raw: '/dev/sda',
description: '',
error: null,
size: null,
blockSize: 512,
logicalBlockSize: 512,
mountpoints: [ {
path: '/media/jwentz/Temp',
label: undefined
} ],
isReadOnly: false,
isSystem: false,
isVirtual: null,
isRemovable: true,
isCard: null,
isSCSI: null,
isUSB: null,
isUAS: null
}, {
enumerator: 'lsblk:json',
busType: 'UNKNOWN',
busVersion: null,
device: '/dev/nvme0n1',
raw: '/dev/nvme0n1',
description: '([SWAP], /boot/efi, /)',
error: null,
size: null,
blockSize: 512,
logicalBlockSize: 512,
mountpoints: [ {
path: '[SWAP]',
label: undefined
}, {
path: '/boot/efi',
label: undefined
}, {
path: '/',
label: undefined
} ],
isReadOnly: false,
isSystem: true,
isVirtual: null,
isRemovable: false,
isCard: null,
isSCSI: null,
isUSB: null,
isUAS: null
} ];

inspect(actual);

m.chai.expect(actual).to.deep.equal(expected);

});

});

});

0 comments on commit d75c06e

Please sign in to comment.