-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create version checker to remove possability of forgetting to update …
…postinstall.js with a version change.
- Loading branch information
1 parent
59f34c1
commit cef81d6
Showing
4 changed files
with
198 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
const { isUpgrade, getLatestVersion, isVersionNewerThanPackagedVersion, getPackagedVersion } = require( "../utils/versionChecker" ); | ||
|
||
const isJSON = require( "../utils/isJSON" ); | ||
const myPkg = require( "../package.json" ); | ||
|
||
describe( "Quick Testing Setup", ( ) => | ||
{ | ||
it( "isJSON should be a function", ( ) => | ||
{ | ||
assert.isFunction( isJSON, "isJSON is not a function" ); | ||
}); | ||
|
||
it( "myPkg should be a JSON object", ( ) => | ||
{ | ||
assert.isTrue( isJSON( myPkg ), "myPkg is not a JSON object" ); | ||
}); | ||
}); | ||
|
||
describe( "Testing versionChecker init", ( ) => | ||
{ | ||
it( "isUpgrade should be a function", ( ) => | ||
{ | ||
assert.isFunction( isUpgrade, "isUpgrade is not a function" ); | ||
}); | ||
|
||
it( "getLatestVersion should be a function", ( ) => | ||
{ | ||
assert.isFunction( getLatestVersion, "getLatestVersion is not a function" ); | ||
}); | ||
|
||
it( "isVersionNewerThanPackagedVersion should be a function", ( ) => | ||
{ | ||
assert.isFunction( isVersionNewerThanPackagedVersion, "isVersionNewerThanPackagedVersion is not a function" ); | ||
}); | ||
|
||
it( "getPackagedVersion should be a function", ( ) => | ||
{ | ||
assert.isFunction( getPackagedVersion, "getPackagedVersion is not a function" ); | ||
}); | ||
}); | ||
|
||
|
||
describe( "Testing versionChecker functionality", ( ) => | ||
{ | ||
it( "getPackagedVersion should return: " + myPkg.version, ( ) => | ||
{ | ||
let result = getPackagedVersion( ); | ||
assert.equal( result, myPkg.version, "getPackagedVersion expected: " + myPkg.version + " found: " + result ); | ||
}); | ||
|
||
it( "getLatestVersion should return: " + myPkg.version, async ( ) => | ||
{ | ||
let result = await getLatestVersion( ); | ||
assert.equal( result, myPkg.version, "getLatestVersion expected: " + myPkg.version + " found: " + result ); | ||
}).timeout(5000); | ||
|
||
it( "isVersionNewerThanPackagedVersion should return a true for a high version", ( ) => | ||
{ | ||
let result = isVersionNewerThanPackagedVersion( "9.0.0" ); | ||
assert.isTrue( result, "isVersionNewerThanPackagedVersion expected: true: found: " + result ); | ||
}); | ||
it( "isVersionNewerThanPackagedVersion should return a false for a lower version", ( ) => | ||
{ | ||
let result = isVersionNewerThanPackagedVersion( "1.0.0" ); | ||
assert.isFalse( result, "isVersionNewerThanPackagedVersion expected: false: found: " + result ); | ||
}); | ||
|
||
it( "isVersionNewerThanPackagedVersion should return a false for same version", ( ) => | ||
{ | ||
let result = isVersionNewerThanPackagedVersion( myPkg.version ); | ||
assert.isFalse( result, "isVersionNewerThanPackagedVersion expected: false: found: " + result ); | ||
}); | ||
|
||
it( "isUpgrade should return: false/true depending on package.version", async ( ) => | ||
{ | ||
// See if upgrade is needed | ||
let result = await isUpgrade( ); | ||
|
||
// Get the latest version. This would have already passed above. | ||
let lv = await getLatestVersion( ); | ||
|
||
// Handle possablility of checking package.json that is not upgraded yet | ||
// NOTE: It is possible that myPkg.version is higher, which may fail as this is not checked | ||
if ( myPkg.version == lv ) | ||
{ | ||
assert.isFalse( result, "isUpgrade expected: false as: myPkg.version=" + myPkg.version + " latestVersion=: " + lv + " Found: " + result ); | ||
} else { | ||
assert.isTrue( result, "isUpgrade expected: true as: myPkg.version=" + myPkg.version + " latestVersion=: " + lv + " Found: " + result ); | ||
} | ||
}).timeout(5000); | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
// These routines are used to get this packages | ||
// version information. | ||
// It not only uses Promises but creates Promises | ||
// and is documented as such. | ||
|
||
const latestVersion = require( "latest-version" ); | ||
|
||
// Retrieve the package information that contains | ||
// things like the current version. | ||
const myPkg = require( "../package.json" ); | ||
|
||
// Split the version into its sub components. | ||
function splitVersion( version ) | ||
{ | ||
let parts = version.split( "." ); | ||
return { "version": parts[ 0 ], "major": parts[ 1 ], "minor": parts[ 2 ] }; | ||
} | ||
|
||
// getLatestVersion could just be defined as: | ||
// function getLatestVersion ( ) | ||
// However, defining it this way signifies it | ||
// returns a Promise. In this case the Promise | ||
// given to us by latestVersion. | ||
const getLatestVersion = async ( ) => | ||
{ | ||
return latestVersion( myPkg.name ); | ||
} | ||
|
||
function getPackagedVersion( ) | ||
{ | ||
return myPkg.version; | ||
} | ||
|
||
// Check that there is a possible upgrade out there. | ||
function isVersionNewerThanPackagedVersion( version ) | ||
{ | ||
// The default return code. | ||
let rc = false; | ||
|
||
// Split the version components into their sub components | ||
let oldVersionInfo = splitVersion( myPkg.version ); | ||
let newVersionInfo = splitVersion( version ); | ||
|
||
// Set the return code appropriately | ||
if ( newVersionInfo.version > oldVersionInfo.version ) | ||
rc = true; | ||
else if ( newVersionInfo.major > oldVersionInfo.major ) | ||
rc = true; | ||
else if ( newVersionInfo.minor > oldVersionInfo.minor ) | ||
rc = true; | ||
return rc; | ||
} | ||
|
||
|
||
// Check that there is a possible upgrade out there. | ||
function isUpgrade( ) | ||
{ | ||
// Create a new Promise that will be fufilled when we processed the | ||
// information provided to us by the Promise of getLatestVersion. | ||
// You cannot take an asynchronous call and convert it to a synchronous | ||
// call unless you would create a timer and wait forever? for it | ||
// to complete, which defeats the purpose of node.js. | ||
return new Promise( ( resolve ) => | ||
{ | ||
// To use the promise of getLatestVersion, it must be in an async function | ||
// so put it in one. | ||
( async( ) => | ||
{ | ||
// Wait for the Promise of getLatestVersion to complete | ||
let lv = await getLatestVersion( ); | ||
|
||
resolve( isVersionNewerThanPackagedVersion( lv ) ); | ||
} | ||
)( ); | ||
}); | ||
} | ||
|
||
// Export the internal functions we wish to expose. | ||
module.exports = { isUpgrade, getLatestVersion, isVersionNewerThanPackagedVersion, getPackagedVersion }; |