@@ -7,82 +7,16 @@ const fs = require('fs')
77const util = require ( 'util' )
88const os = require ( 'os' )
99const path = require ( 'path' )
10- const https = require ( 'https' )
11- const url = require ( 'url' )
1210const { once } = require ( 'events' )
1311const { expect } = require ( 'chai' )
12+ const latests = require ( '../plugins/versions/package.json' ) . dependencies
1413
1514process . env . DD_INSTRUMENTATION_TELEMETRY_ENABLED = 'false'
1615
1716const mkdtemp = util . promisify ( fs . mkdtemp )
1817
1918const ddTraceInit = path . resolve ( __dirname , '../../../../init' )
2019
21- const latestCache = [ ]
22- async function getLatest ( modName , repoUrl ) {
23- if ( latestCache [ modName ] ) {
24- return latestCache [ modName ]
25- }
26- const { stdout } = await retry ( ( ) => exec ( `npm view ${ modName } dist-tags --json` ) , 1000 )
27- const { latest } = JSON . parse ( stdout )
28- const tags = await get ( `https://api.github.com/repos/${ repoUrl } /git/refs/tags` )
29- for ( const tag of tags ) {
30- if ( tag . ref . includes ( latest ) ) {
31- return tag . ref . split ( '/' ) . pop ( )
32- }
33- }
34- }
35-
36- function get ( theUrl ) {
37- return new Promise ( ( resolve , reject ) => {
38- // eslint-disable-next-line n/no-deprecated-api
39- const options = url . parse ( theUrl )
40- options . headers = {
41- 'user-agent' : 'dd-trace plugin test suites'
42- }
43- https . get ( options , res => {
44- if ( res . statusCode === 403 ) {
45- console . log ( '403' )
46- for ( const header in res . headers ) {
47- if ( header . startsWith ( 'x-ratelimit' ) ) {
48- console . log ( header , res . headers [ header ] )
49- }
50- }
51- const resetTime = Number ( res . headers [ 'x-ratelimit-reset' ] ) * 1000
52- const waitTime = 1000 + resetTime - Date . now ( )
53- console . log ( 'Waiting' , waitTime / 1000 , 'seconds for retry' )
54- setTimeout ( ( ) => {
55- get ( theUrl ) . then ( resolve , reject )
56- } , waitTime )
57- return
58- }
59- if ( res . statusCode >= 300 ) {
60- res . pipe ( process . stderr )
61- reject ( new Error ( res . statusCode ) )
62- return
63- }
64- const data = [ ]
65- res . on ( 'data' , d => data . push ( d ) )
66- res . on ( 'end' , ( ) => {
67- resolve ( JSON . parse ( Buffer . concat ( data ) . toString ( 'utf8' ) ) )
68- } )
69- } ) . on ( 'error' , reject )
70- } )
71- }
72-
73- async function retry ( fn , delay ) {
74- let result
75- try {
76- result = fn ( )
77- } catch ( e ) {
78- console . log ( e )
79- console . log ( 'Retrying after' , delay , 'ms' )
80- await new Promise ( resolve => setTimeout ( resolve , delay ) )
81- result = retry ( fn , delay )
82- }
83- return result
84- }
85-
8620function exec ( cmd , opts = { } ) {
8721 const date = new Date ( )
8822 const time = [
@@ -127,11 +61,18 @@ function getTmpDir () {
12761
12862async function setup ( modName , repoName , commitish ) {
12963 if ( commitish === 'latest' ) {
130- commitish = await getLatest ( modName , repoName )
64+ commitish = latests [ modName ]
13165 }
13266 const repoUrl = `https://github.com/${ repoName } .git`
13367 const cwd = await getTmpDir ( )
134- await execOrError ( `git clone ${ repoUrl } --branch ${ commitish } --single-branch ${ cwd } ` )
68+ const clone = `git clone ${ repoUrl } --single-branch ${ cwd } `
69+
70+ try {
71+ await execOrError ( `${ clone } --branch ${ commitish } ` )
72+ } catch {
73+ // Exact version doesn't exist, try with a `v` prefix for example `v1.2.3`.
74+ await execOrError ( `${ clone } --branch v${ commitish } ` )
75+ }
13576
13677 try {
13778 await execOrError ( 'npm install --legacy-peer-deps' , { cwd } )
0 commit comments