2021-12-17 00:17:02 +00:00
import { execa } from "execa" ;
2021-04-20 18:48:48 +00:00
import restana from "restana" ;
2022-12-09 11:46:05 +00:00
import { join , dirname } from "node:path" ;
import { readFileSync , mkdtempSync } from "node:fs" ;
import { writeFile , readFile , rm } from "node:fs/promises" ;
import { fileURLToPath } from "node:url" ;
import { tmpdir } from "node:os" ;
2019-01-19 19:05:26 +00:00
2022-10-26 13:48:08 +00:00
const testFile = fileURLToPath ( new URL ( "fixtures/test.json" , import . meta . url ) ) ;
const emptyFile = fileURLToPath ( new URL ( "fixtures/empty.json" , import . meta . url ) ) ;
2021-04-20 18:48:48 +00:00
const testPkg = JSON . parse ( readFileSync ( testFile , "utf8" ) ) ;
2022-10-26 13:23:15 +00:00
const testDir = mkdtempSync ( join ( tmpdir ( ) , "updates-" ) ) ;
2022-10-26 13:48:08 +00:00
const script = fileURLToPath ( new URL ( "bin/updates.js" , import . meta . url ) ) ;
2019-12-18 17:19:05 +00:00
2020-03-08 09:56:31 +00:00
const dependencyTypes = [
"dependencies" ,
"devDependencies" ,
"peerDependencies" ,
"optionalDependencies" ,
2020-10-21 18:22:26 +00:00
"resolutions" ,
2020-03-08 09:23:44 +00:00
] ;
2019-12-18 17:19:05 +00:00
2020-03-15 00:14:12 +00:00
const testPackages = new Set ( ) ;
2020-03-08 09:56:31 +00:00
for ( const dependencyType of dependencyTypes ) {
2020-07-14 22:01:04 +00:00
for ( const name of Object . keys ( testPkg [ dependencyType ] || [ ] ) ) {
2020-03-15 00:14:12 +00:00
testPackages . add ( name ) ;
2020-03-08 09:56:31 +00:00
}
}
2020-04-18 12:54:05 +00:00
function makeUrl ( server ) {
2020-10-05 21:26:38 +00:00
const { port } = server . address ( ) ;
2020-10-21 18:22:26 +00:00
return Object . assign ( new URL ( "http://localhost" ) , { port } ) . toString ( ) ;
2020-04-18 12:54:05 +00:00
}
2020-07-21 18:38:38 +00:00
function defaultRoute ( req , res ) {
console . error ( ` default handler hit for ${ req . url } ` ) ;
res . send ( 404 ) ;
}
2020-10-21 18:22:26 +00:00
function resolutionsBasePackage ( name ) {
const packages = name . match ( /(@[^/]+\/)?([^/]+)/g ) || [ ] ;
return packages [ packages . length - 1 ] ;
}
2020-03-15 00:14:12 +00:00
let npmServer , githubServer , githubUrl , npmUrl ;
2020-03-08 09:23:44 +00:00
beforeAll ( async ( ) => {
2020-03-09 22:57:04 +00:00
let commits , tags ;
[ npmServer , githubServer , commits , tags ] = await Promise . all ( [
2020-07-21 18:38:38 +00:00
restana ( { defaultRoute } ) ,
restana ( { defaultRoute } ) ,
2022-10-26 13:48:08 +00:00
readFile ( fileURLToPath ( new URL ( "fixtures/github/updates-commits.json" , import . meta . url ) ) ) ,
readFile ( fileURLToPath ( new URL ( "fixtures/github/updates-tags.json" , import . meta . url ) ) ) ,
2020-03-09 22:57:04 +00:00
] ) ;
2020-03-08 19:04:37 +00:00
2020-10-21 18:22:26 +00:00
for ( const pkgName of testPackages ) {
const name = testPkg . resolutions [ pkgName ] ? resolutionsBasePackage ( pkgName ) : pkgName ;
const urlName = name . replace ( /\//g , "%2f" ) ;
2022-10-26 13:48:08 +00:00
// can not use file URLs because node stupidely throws on "%2f" in paths.
const path = join ( dirname ( fileURLToPath ( import . meta . url ) ) , ` fixtures/npm/ ${ urlName } .json ` ) ;
2020-10-21 18:22:26 +00:00
npmServer . get ( ` / ${ urlName } ` , async ( _ , res ) => res . send ( await readFile ( path ) ) ) ;
2019-12-18 17:19:05 +00:00
}
2020-07-21 18:54:05 +00:00
githubServer . get ( "/repos/silverwind/updates/commits" , ( _ , res ) => res . send ( commits ) ) ;
githubServer . get ( "/repos/silverwind/updates/git/refs/tags" , ( _ , res ) => res . send ( tags ) ) ;
2020-03-15 00:14:12 +00:00
2020-04-18 12:54:05 +00:00
[ githubServer , npmServer ] = await Promise . all ( [
githubServer . start ( 0 ) ,
npmServer . start ( 0 ) ,
2020-03-15 00:14:12 +00:00
] ) ;
2020-03-08 18:12:16 +00:00
2020-04-18 12:54:05 +00:00
githubUrl = makeUrl ( githubServer ) ;
npmUrl = makeUrl ( npmServer ) ;
2020-03-15 00:14:12 +00:00
await writeFile ( join ( testDir , ".npmrc" ) , ` registry= ${ npmUrl } ` ) ; // Fake registry
2020-07-14 22:01:04 +00:00
await writeFile ( join ( testDir , "package.json" ) , JSON . stringify ( testPkg , null , 2 ) ) ; // Copy fixture
2020-03-08 09:23:44 +00:00
} ) ;
2019-12-18 17:19:05 +00:00
2020-03-08 09:23:44 +00:00
afterAll ( async ( ) => {
2020-03-08 18:12:16 +00:00
await Promise . all ( [
2022-11-23 20:45:54 +00:00
rm ( testDir , { recursive : true } ) ,
2022-10-26 13:23:15 +00:00
npmServer ? . close ( ) ,
githubServer ? . close ( ) ,
2020-03-08 18:12:16 +00:00
] ) ;
2020-03-08 09:23:44 +00:00
} ) ;
2019-01-19 19:05:26 +00:00
2020-03-08 09:23:44 +00:00
function makeTest ( args , expected ) {
return async ( ) => {
2020-07-14 22:01:04 +00:00
const argsArr = [ ... args . split ( /\s+/ ) , "-c" , "-G" , githubUrl ] ;
const { stdout } = await execa ( script , argsArr , { cwd : testDir } ) ;
2020-03-08 16:06:58 +00:00
const { results } = JSON . parse ( stdout ) ;
2020-03-08 16:44:04 +00:00
// Parse results, with custom validation for the dynamic "age" property
2020-03-08 16:06:58 +00:00
for ( const dependencyType of dependencyTypes ) {
2022-11-10 21:02:48 +00:00
for ( const name of Object . keys ( results [ dependencyType ] || { } ) ) {
delete results [ dependencyType ] [ name ] . age ;
2020-03-08 16:06:58 +00:00
}
}
2022-11-10 21:02:48 +00:00
expect ( results ) . toEqual ( expected ) ;
2020-03-08 09:23:44 +00:00
} ;
2019-01-19 19:05:26 +00:00
}
2022-09-25 22:17:29 +00:00
test ( "version" , async ( ) => {
const { version : expected } = JSON . parse ( readFileSync ( new URL ( "package.json" , import . meta . url ) , "utf8" ) ) ;
const { stdout , exitCode } = await execa ( "node" , [ script , "-v" ] ) ;
expect ( stdout ) . toEqual ( expected ) ;
expect ( exitCode ) . toEqual ( 0 ) ;
} ) ;
2020-07-14 22:01:04 +00:00
test ( "simple" , async ( ) => {
2020-07-21 18:54:05 +00:00
const { stdout , stderr , exitCode } = await execa ( script , [ "-C" , "-G" , githubUrl , "-f" , testFile ] ) ;
2020-07-14 22:01:04 +00:00
expect ( stderr ) . toEqual ( "" ) ;
2022-10-17 21:36:44 +00:00
expect ( stdout ) . toContain ( "prismjs" ) ;
expect ( stdout ) . toContain ( "https://github.com/silverwind/updates" ) ;
2020-07-14 22:01:04 +00:00
expect ( exitCode ) . toEqual ( 0 ) ;
} ) ;
2022-10-26 13:36:46 +00:00
test ( "empty" , async ( ) => {
const { stdout , stderr , exitCode } = await execa ( script , [ "-C" , "-G" , githubUrl , "-f" , emptyFile ] ) ;
expect ( stderr ) . toEqual ( "" ) ;
2022-10-26 16:22:10 +00:00
expect ( stdout ) . toContain ( "No dependencies" ) ;
2022-10-26 13:36:46 +00:00
expect ( exitCode ) . toEqual ( 0 ) ;
} ) ;
2021-04-20 19:29:45 +00:00
test ( "version" , async ( ) => {
const { stdout , stderr , exitCode } = await execa ( script , [ "-v" ] ) ;
expect ( stderr ) . toEqual ( "" ) ;
expect ( stdout ) . toMatch ( /^[0-9]+\.[0-9]+\.[0-9]+$/ ) ;
expect ( exitCode ) . toEqual ( 0 ) ;
} ) ;
2020-07-21 20:41:43 +00:00
if ( process . env . CI ) {
test ( "global" , async ( ) => {
await execa ( "npm" , [ "i" , "-g" , "." ] ) ;
const { stdout , stderr , exitCode } = await execa ( "updates" , [ "-C" , "-G" , githubUrl , "-f" , testFile ] ) ;
expect ( stderr ) . toEqual ( "" ) ;
2022-10-17 21:36:44 +00:00
expect ( stdout ) . toContain ( "prismjs" ) ;
expect ( stdout ) . toContain ( "https://github.com/silverwind/updates" ) ;
2020-07-21 20:41:43 +00:00
expect ( exitCode ) . toEqual ( 0 ) ;
} ) ;
}
2020-03-08 09:56:31 +00:00
test ( "latest" , makeTest ( "-j" , {
2020-03-08 09:23:44 +00:00
dependencies : {
"gulp-sourcemaps" : {
old : "2.0.0" ,
new : "2.6.5" ,
info : "https://github.com/gulp-sourcemaps/gulp-sourcemaps" ,
} ,
"prismjs" : {
old : "1.0.0" ,
new : "1.17.1" ,
info : "https://github.com/LeaVerou/prism" ,
} ,
"svgstore" : {
old : "^3.0.0" ,
new : "^3.0.0-2" ,
info : "https://github.com/svgstore/svgstore" ,
} ,
"html-webpack-plugin" : {
old : "4.0.0-alpha.2" ,
new : "4.0.0-beta.11" ,
info : "https://github.com/jantimon/html-webpack-plugin" ,
} ,
"noty" : {
old : "3.1.0" ,
new : "3.2.0-beta" ,
info : "https://github.com/needim/noty" ,
} ,
"jpeg-buffer-orientation" : {
old : "0.0.0" ,
new : "2.0.3" ,
info : "https://github.com/fisker/jpeg-buffer-orientation" ,
} ,
"styled-components" : {
old : "2.5.0-1" ,
new : "5.0.0-rc.2" ,
info : "https://github.com/styled-components/styled-components" ,
} ,
"@babel/preset-env" : {
old : "7.0.0" ,
2020-10-03 12:50:52 +00:00
new : "7.11.5" ,
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
2020-03-08 18:12:16 +00:00
} ,
"updates" : {
old : "6941e05" ,
new : "537ccb7" ,
info : "https://github.com/silverwind/updates" ,
} ,
2022-11-10 21:02:48 +00:00
"react" : {
2022-11-10 21:44:24 +00:00
old : "18.0" ,
2022-11-10 21:02:48 +00:00
new : "18.2.0" ,
info : "https://github.com/facebook/react/tree/HEAD/packages/react" ,
} ,
2020-03-08 09:23:44 +00:00
} ,
peerDependencies : {
"@babel/preset-env" : {
2020-03-09 21:00:55 +00:00
old : "~6.0.0" ,
2020-10-03 12:50:52 +00:00
new : "~7.11.5" ,
2020-10-21 18:22:26 +00:00
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
} ,
} ,
resolutions : {
"versions/updates" : {
old : "^1.0.0" ,
new : "^10.0.0" ,
info : "https://github.com/silverwind/updates" ,
} ,
2020-03-08 09:23:44 +00:00
} ,
} ) ) ;
2019-01-20 21:34:40 +00:00
2020-03-08 09:56:31 +00:00
test ( "greatest" , makeTest ( "-j -g" , {
2020-03-08 09:23:44 +00:00
dependencies : {
"gulp-sourcemaps" : {
old : "2.0.0" ,
new : "2.6.5" ,
info : "https://github.com/gulp-sourcemaps/gulp-sourcemaps" ,
} ,
"prismjs" : {
old : "1.0.0" ,
new : "1.17.1" ,
info : "https://github.com/LeaVerou/prism" ,
} ,
"html-webpack-plugin" : {
old : "4.0.0-alpha.2" ,
new : "4.0.0-beta.11" ,
info : "https://github.com/jantimon/html-webpack-plugin" ,
} ,
"noty" : {
old : "3.1.0" ,
new : "3.1.4" ,
info : "https://github.com/needim/noty" ,
} ,
"jpeg-buffer-orientation" : {
old : "0.0.0" ,
new : "2.0.3" ,
info : "https://github.com/fisker/jpeg-buffer-orientation" ,
} ,
"styled-components" : {
old : "2.5.0-1" ,
new : "5.0.0-rc.2" ,
info : "https://github.com/styled-components/styled-components" ,
} ,
"@babel/preset-env" : {
old : "7.0.0" ,
2020-10-03 12:50:52 +00:00
new : "7.11.5" ,
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
2020-03-08 18:12:16 +00:00
} ,
"updates" : {
old : "6941e05" ,
new : "537ccb7" ,
info : "https://github.com/silverwind/updates" ,
} ,
2022-11-10 21:02:48 +00:00
"react" : {
2022-11-10 21:44:24 +00:00
old : "18.0" ,
2022-11-10 21:02:48 +00:00
new : "18.2.0" ,
info : "https://github.com/facebook/react/tree/HEAD/packages/react" ,
}
2020-03-08 09:23:44 +00:00
} ,
peerDependencies : {
"@babel/preset-env" : {
2020-03-09 21:00:55 +00:00
old : "~6.0.0" ,
2020-10-03 12:50:52 +00:00
new : "~7.11.5" ,
2020-10-21 18:22:26 +00:00
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
} ,
} ,
resolutions : {
"versions/updates" : {
old : "^1.0.0" ,
new : "^10.0.0" ,
info : "https://github.com/silverwind/updates" ,
} ,
} ,
2020-03-08 09:23:44 +00:00
} ) ) ;
2019-02-25 21:46:39 +00:00
2020-03-08 09:56:31 +00:00
test ( "prerelease" , makeTest ( "-j -g -p" , {
2020-03-08 09:23:44 +00:00
dependencies : {
"gulp-sourcemaps" : {
old : "2.0.0" ,
new : "2.6.5" ,
info : "https://github.com/gulp-sourcemaps/gulp-sourcemaps" ,
} ,
"prismjs" : {
old : "1.0.0" ,
new : "1.17.1" ,
info : "https://github.com/LeaVerou/prism" ,
} ,
"svgstore" : {
old : "^3.0.0" ,
new : "^3.0.0-2" ,
info : "https://github.com/svgstore/svgstore" ,
} ,
"html-webpack-plugin" : {
old : "4.0.0-alpha.2" ,
new : "4.0.0-beta.11" ,
info : "https://github.com/jantimon/html-webpack-plugin" ,
} ,
"noty" : {
old : "3.1.0" ,
new : "3.2.0-beta" ,
info : "https://github.com/needim/noty" ,
} ,
"jpeg-buffer-orientation" : {
old : "0.0.0" ,
new : "2.0.3" ,
info : "https://github.com/fisker/jpeg-buffer-orientation" ,
} ,
"styled-components" : {
old : "2.5.0-1" ,
new : "5.0.0-rc.2" ,
info : "https://github.com/styled-components/styled-components" ,
} ,
"@babel/preset-env" : {
old : "7.0.0" ,
2020-10-03 12:50:52 +00:00
new : "7.11.5" ,
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
2020-03-08 18:12:16 +00:00
} ,
"updates" : {
old : "6941e05" ,
new : "537ccb7" ,
info : "https://github.com/silverwind/updates" ,
} ,
2022-11-10 21:02:48 +00:00
"react" : {
2022-11-10 21:44:24 +00:00
old : "18.0" ,
2022-11-10 21:02:48 +00:00
new : "18.3.0-next-d1e35c703-20221110" ,
info : "https://github.com/facebook/react/tree/HEAD/packages/react" ,
}
2020-03-08 09:23:44 +00:00
} ,
peerDependencies : {
"@babel/preset-env" : {
2020-03-09 21:00:55 +00:00
old : "~6.0.0" ,
2020-10-03 12:50:52 +00:00
new : "~7.11.5" ,
2020-10-21 18:22:26 +00:00
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
} ,
} ,
resolutions : {
"versions/updates" : {
old : "^1.0.0" ,
new : "^10.0.0" ,
info : "https://github.com/silverwind/updates" ,
} ,
2020-03-08 09:23:44 +00:00
} ,
} ) ) ;
2019-03-28 20:22:49 +00:00
2020-03-08 09:56:31 +00:00
test ( "release" , makeTest ( "-j -R" , {
2020-03-08 09:23:44 +00:00
dependencies : {
"gulp-sourcemaps" : {
old : "2.0.0" ,
new : "2.6.5" ,
info : "https://github.com/gulp-sourcemaps/gulp-sourcemaps" ,
} ,
"prismjs" : {
old : "1.0.0" ,
new : "1.17.1" ,
info : "https://github.com/LeaVerou/prism" ,
} ,
"svgstore" : {
old : "^3.0.0" ,
new : "^2.0.3" ,
info : "https://github.com/svgstore/svgstore" ,
} ,
"html-webpack-plugin" : {
old : "4.0.0-alpha.2" ,
new : "3.2.0" ,
info : "https://github.com/jantimon/html-webpack-plugin" ,
} ,
"noty" : {
old : "3.1.0" ,
new : "3.1.4" ,
info : "https://github.com/needim/noty" ,
} ,
"jpeg-buffer-orientation" : {
old : "0.0.0" ,
new : "2.0.3" ,
info : "https://github.com/fisker/jpeg-buffer-orientation" ,
} ,
"styled-components" : {
old : "2.5.0-1" ,
new : "4.4.1" ,
info : "https://github.com/styled-components/styled-components" ,
} ,
"@babel/preset-env" : {
old : "7.0.0" ,
2020-10-03 12:50:52 +00:00
new : "7.11.5" ,
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
2020-03-08 18:12:16 +00:00
} ,
"updates" : {
old : "6941e05" ,
new : "537ccb7" ,
info : "https://github.com/silverwind/updates" ,
} ,
2022-11-10 21:02:48 +00:00
"react" : {
2022-11-10 21:44:24 +00:00
old : "18.0" ,
2022-11-10 21:02:48 +00:00
new : "18.2.0" ,
info : "https://github.com/facebook/react/tree/HEAD/packages/react" ,
}
2020-03-08 09:23:44 +00:00
} ,
peerDependencies : {
"@babel/preset-env" : {
2020-03-09 21:00:55 +00:00
old : "~6.0.0" ,
2020-10-03 12:50:52 +00:00
new : "~7.11.5" ,
2020-10-21 18:22:26 +00:00
info : "https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env" ,
} ,
} ,
resolutions : {
"versions/updates" : {
old : "^1.0.0" ,
new : "^10.0.0" ,
info : "https://github.com/silverwind/updates" ,
2020-03-08 18:12:16 +00:00
} ,
2020-03-08 09:23:44 +00:00
} ,
} ) ) ;
2019-01-19 19:05:26 +00:00
2020-03-08 09:56:31 +00:00
test ( "patch" , makeTest ( "-j -P" , {
2020-03-08 09:23:44 +00:00
dependencies : {
"gulp-sourcemaps" : {
old : "2.0.0" ,
new : "2.0.1" ,
info : "https://github.com/floridoo/gulp-sourcemaps" ,
} ,
"svgstore" : {
old : "^3.0.0" ,
new : "^3.0.0-2" ,
info : "https://github.com/svgstore/svgstore" ,
} ,
"html-webpack-plugin" : {
old : "4.0.0-alpha.2" ,
new : "4.0.0-beta.11" ,
info : "https://github.com/jantimon/html-webpack-plugin" ,
} ,
"noty" : {
old : "3.1.0" ,
new : "3.1.4" ,
info : "https://github.com/needim/noty" ,
} ,
2020-03-08 18:12:16 +00:00
"updates" : {
old : "6941e05" ,
new : "537ccb7" ,
info : "https://github.com/silverwind/updates" ,
} ,
2020-03-08 09:23:44 +00:00
} ,
2020-10-21 18:22:26 +00:00
resolutions : {
"versions/updates" : {
old : "^1.0.0" ,
new : "^1.0.6" ,
info : "https://github.com/silverwind/updates" ,
} ,
} ,
2020-03-08 09:23:44 +00:00
} ) ) ;
2020-03-09 21:24:57 +00:00
test ( "include version deps" , makeTest ( "-j -i noty" , {
dependencies : {
"noty" : {
old : "3.1.0" ,
new : "3.2.0-beta" ,
info : "https://github.com/needim/noty" ,
} ,
} ,
} ) ) ;
2020-04-20 09:09:28 +00:00
test ( "include version deps #2" , makeTest ( "-j -i noty -i noty,noty" , {
dependencies : {
"noty" : {
old : "3.1.0" ,
new : "3.2.0-beta" ,
info : "https://github.com/needim/noty" ,
} ,
} ,
} ) ) ;
2022-11-10 21:02:48 +00:00
test ( "exclude version deps" , makeTest ( "-j -e gulp-sourcemaps,prismjs,svgstore,html-webpack-plugin,noty,jpeg-buffer-orientation,styled-components,@babel/preset-env,versions/updates,react" , {
2020-03-09 21:24:57 +00:00
dependencies : {
"updates" : {
old : "6941e05" ,
new : "537ccb7" ,
info : "https://github.com/silverwind/updates" ,
} ,
} ,
} ) ) ;