Skip to content

Commit

Permalink
#1004: Add testable way to extract the host path from a volume
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Jun 1, 2018
1 parent 732ad3a commit 079da97
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 35 deletions.
49 changes: 25 additions & 24 deletions docs/SUMMARY.md
Expand Up @@ -254,30 +254,31 @@
* [lando.updates.refresh](api/api.md#landoupdatesrefresh)
* [lando.user.getGid](api/api.md#landousergetgid)
* [lando.user.getUid](api/api.md#landousergetuid)
* [lando.util.app.appNameExists](api/api.md#landoutilsappappnameexists)
* [lando.util.app.compose](api/api.md#landoutilsappcompose)
* [lando.util.app.getInfoDefaults](api/api.md#landoutilsappgetinfodefaults)
* [lando.util.app.getUrls](api/api.md#landoutilsappgeturls)
* [lando.util.app.metricsParse](api/api.md#landoutilsappmetricsparse)
* [lando.util.app.startTable](api/api.md#landoutilsappstarttable)
* [lando.util.app.validateFiles](api/api.md#landoutilsappvalidatefiles)
* [lando.util.config.defaults](api/api.md#landoutilsconfigdefaults)
* [lando.util.config.loadFiles](api/api.md#landoutilsconfigloadfiles)
* [lando.util.config.loadEnvs](api/api.md#landoutilsconfigloadenvs)
* [lando.util.config.merge](api/api.md#landoutilsconfigmerge)
* [lando.util.config.stripEnv](api/api.md#landoutilsconfigstripenv)
* [lando.util.config.tryConvertJson](api/api.md#landoutilsconfigtryconfigjson)
* [lando.util.engine.dockerComposify](api/api.md#landoutilsenginedockercomposify)
* [lando.util.engine.moveConfig](api/api.md#landoutilsenginemoveconfig)
* [lando.util.engine.normalizer](api/api.md#landoutilsenginenormalizer)
* [lando.util.services.addConfig](api/api.md#landoutilsservicesaddconfig)
* [lando.util.services.addScript](api/api.md#landoutilsservicesaddscript)
* [lando.util.services.buildVolume](api/api.md#landoutilsservicesbuildvolume)
* [lando.util.services.connectBridge](api/api.md#landoutilsservicesconnectbridge)
* [lando.util.services.connectNet](api/api.md#landoutilsservicesconnectnridge)
* [lando.util.services.filterBuildSteps](api/api.md#landoutilsservicesfilterbuildsteps)
* [lando.util.services.normalizePath](api/api.md#landoutilsservicesnormalizepath)
* [lando.util.services.setEntrypoint](api/api.md#landoutilsservicessetentrypoint)
* [lando.utils.app.appNameExists](api/api.md#landoutilssappappnameexists)
* [lando.utils.app.compose](api/api.md#landoutilssappcompose)
* [lando.utils.app.getInfoDefaults](api/api.md#landoutilssappgetinfodefaults)
* [lando.utils.app.getUrls](api/api.md#landoutilssappgeturls)
* [lando.utils.app.metricsParse](api/api.md#landoutilssappmetricsparse)
* [lando.utils.app.startTable](api/api.md#landoutilssappstarttable)
* [lando.utils.app.validateFiles](api/api.md#landoutilssappvalidatefiles)
* [lando.utils.config.defaults](api/api.md#landoutilssconfigdefaults)
* [lando.utils.config.loadFiles](api/api.md#landoutilssconfigloadfiles)
* [lando.utils.config.loadEnvs](api/api.md#landoutilssconfigloadenvs)
* [lando.utils.config.merge](api/api.md#landoutilssconfigmerge)
* [lando.utils.config.stripEnv](api/api.md#landoutilssconfigstripenv)
* [lando.utils.config.tryConvertJson](api/api.md#landoutilssconfigtryconfigjson)
* [lando.utils.engine.dockerComposify](api/api.md#landoutilssenginedockercomposify)
* [lando.utils.engine.moveConfig](api/api.md#landoutilssenginemoveconfig)
* [lando.utils.engine.normalizer](api/api.md#landoutilssenginenormalizer)
* [lando.utils.services.addConfig](api/api.md#landoutilssservicesaddconfig)
* [lando.utils.services.addScript](api/api.md#landoutilssservicesaddscript)
* [lando.utils.services.buildVolume](api/api.md#landoutilssservicesbuildvolume)
* [lando.utils.services.connectBridge](api/api.md#landoutilssservicesconnectbridge)
* [lando.utils.services.connectNet](api/api.md#landoutilssservicesconnectnridge)
* [lando.utils.services.filterBuildSteps](api/api.md#landoutilssservicesfilterbuildsteps)
* [lando.utils.services.getHostPath](api/api.md#landoutilssservicesgethostpath)
* [lando.utils.services.normalizePath](api/api.md#landoutilssservicesnormalizepath)
* [lando.utils.services.setEntrypoint](api/api.md#landoutilssservicessetentrypoint)
* [lando.yaml.dump](api/api.md#landoyamldump)
* [lando.yaml.load](api/api.md#landoyamlload)
* [events.pre-bootstrap](api/api.md#event_pre_bootstrap)
Expand Down
5 changes: 3 additions & 2 deletions plugins/lando-engine/lib/compose.js
Expand Up @@ -137,8 +137,9 @@ var buildCmd = function(compose, project, run, opts) {
});
}

// Add in a command arg if its there
if (opts && opts.cmd) {
// Add a hacky conditional here because windoze seems to handle this weirdly
// @todo: remove the above during unit testing
if (opts && opts.cmd && run === 'exec') {
if (typeof opts.cmd === 'string') {
opts.cmd = [opts.cmd];
}
Expand Down
16 changes: 11 additions & 5 deletions plugins/lando-services/lib/utils.js
Expand Up @@ -143,6 +143,17 @@ exports.filterBuildSteps = function(services) {

};


/**
* Helper method to get the host part of a volume
*
* @since 3.0.0
* @alias 'lando.utils.services.getHostPath'
* @param {String} mount The entire mount
* @returns {String} The host part of the mount
*/
exports.getHostPath = mount => _.dropRight(mount.split(':')).join(':');

/**
* Helper method to normalize a path so that Lando overrides can be used as though
* the docker-compose files were in the app root.
Expand All @@ -155,19 +166,14 @@ exports.filterBuildSteps = function(services) {
* @returns {String} Either local or local prefixed by base
*/
exports.normalizePath = (local, base, excludes = []) => {

// Return local if it starts with $
if (_.startsWith(local, '$')) return local;

// Return local if it is one of the excludes
if (_.includes(excludes, local)) return local;

// Return local if local is an absolute path
if (path.isAbsolute(local)) return local;

// Otherwise this is a relaive path so return local resolved by base
return path.resolve(path.join(base, local));

};

/**
Expand Down
7 changes: 4 additions & 3 deletions plugins/lando-services/services.js
Expand Up @@ -193,10 +193,11 @@ module.exports = function(lando) {
return volume;
}
else {
var parts = volume.split(':');
var local = utils.getHostPath(volume);
var remote = _.last(volume.split(':'));
var excludes = _.keys(volumes).concat(_.keys(overrides.volumes));
var host = utils.normalizePath(parts[0], config._root, excludes);
return [host, parts[1]].join(':');
var host = utils.normalizePath(local, config._root, excludes);
return [host, remote].join(':');
}
});
}
Expand Down
15 changes: 14 additions & 1 deletion plugins/lando-services/test/unit/utils.spec.js
Expand Up @@ -38,6 +38,20 @@ describe('lando-services.utils', () => {
it('should have a completed spec!');
});

// getHostPath method
describe('#getHostPath', () => {

it('should return the correct host path on posix', () => {
const hostPath = utils.getHostPath('/thing:/stuff');
expect(hostPath).to.equal('/thing');
});
it('should return the correct host path on windoze', () => {
const hostPath = utils.getHostPath('C:\\thing:/stuff');
expect(hostPath).to.equal('C:\\thing');
});

});

// normalizePath method
describe('#normalizePath', () => {

Expand Down Expand Up @@ -96,7 +110,6 @@ describe('lando-services.utils', () => {

});


});

// setEntrypoint method
Expand Down

0 comments on commit 079da97

Please sign in to comment.