1
1
const t = require ( 'tap' )
2
- const { load : loadMockNpm } = require ( '../../fixtures/mock-npm' )
2
+ const {
3
+ load : originalLoadMockNpm ,
4
+ mockNpmRegistryFetch,
5
+ putPackagePayload } = require ( '../../fixtures/mock-npm' )
3
6
const { cleanZlib } = require ( '../../fixtures/clean-snapshot' )
4
7
const MockRegistry = require ( '@npmcli/mock-registry' )
5
8
const pacote = require ( 'pacote' )
@@ -22,6 +25,20 @@ const pkgJson = {
22
25
23
26
t . cleanSnapshot = data => cleanZlib ( data )
24
27
28
+ function loadMockNpm ( test , args ) {
29
+ return originalLoadMockNpm ( test , {
30
+ ...args ,
31
+ mocks : {
32
+ ...mockNpmRegistryFetch ( {
33
+ [ `/-/package/${ pkg } /dist-tags` ] : ( ) => {
34
+ throw new Error ( 'not found' )
35
+ } ,
36
+ } ) . mocks ,
37
+ ...args . mocks ,
38
+ } ,
39
+ } )
40
+ }
41
+
25
42
t . test ( 'respects publishConfig.registry, runs appropriate scripts' , async t => {
26
43
const { npm, joinedOutput, prefix } = await loadMockNpm ( t , {
27
44
config : {
@@ -1069,3 +1086,105 @@ t.test('does not abort when prerelease and authored tag latest', async t => {
1069
1086
} ) . reply ( 200 , { } )
1070
1087
await npm . exec ( 'publish' , [ ] )
1071
1088
} )
1089
+
1090
+ t . test ( 'PREVENTS publish when latest dist-tag is HIGHER than publishing version' , async t => {
1091
+ const latest = '100.0.0'
1092
+ const version = '50.0.0'
1093
+
1094
+ const { npm } = await loadMockNpm ( t , {
1095
+ config : {
1096
+ loglevel : 'silent' ,
1097
+ [ `${ alternateRegistry . slice ( 6 ) } /:_authToken` ] : 'test-other-token' ,
1098
+ } ,
1099
+ prefixDir : {
1100
+ 'package.json' : JSON . stringify ( {
1101
+ ...pkgJson ,
1102
+ version,
1103
+ scripts : {
1104
+ prepublishOnly : 'touch scripts-prepublishonly' ,
1105
+ prepublish : 'touch scripts-prepublish' , // should NOT run this one
1106
+ publish : 'touch scripts-publish' ,
1107
+ postpublish : 'touch scripts-postpublish' ,
1108
+ } ,
1109
+ publishConfig : { registry : alternateRegistry } ,
1110
+ } , null , 2 ) ,
1111
+ } ,
1112
+ mocks : {
1113
+ ...mockNpmRegistryFetch ( {
1114
+ [ `/-/package/${ pkg } /dist-tags` ] : { latest } ,
1115
+ } ) . mocks ,
1116
+ } ,
1117
+ } )
1118
+ await t . rejects ( async ( ) => {
1119
+ await npm . exec ( 'publish' , [ ] )
1120
+ } , new Error ( 'Cannot publish a lower version without an explicit dist tag.' ) )
1121
+ } )
1122
+
1123
+ t . test ( 'ALLOWS publish when latest dist-tag is LOWER than publishing version' , async t => {
1124
+ const version = '100.0.0'
1125
+ const latest = '50.0.0'
1126
+
1127
+ const { npm } = await loadMockNpm ( t , {
1128
+ config : {
1129
+ loglevel : 'silent' ,
1130
+ [ `${ alternateRegistry . slice ( 6 ) } /:_authToken` ] : 'test-other-token' ,
1131
+ } ,
1132
+ prefixDir : {
1133
+ 'package.json' : JSON . stringify ( {
1134
+ ...pkgJson ,
1135
+ version,
1136
+ publishConfig : { registry : alternateRegistry } ,
1137
+ } , null , 2 ) ,
1138
+ } ,
1139
+ mocks : {
1140
+ ...mockNpmRegistryFetch ( {
1141
+ [ `/-/package/${ pkg } /dist-tags` ] : { latest } ,
1142
+ } ) . mocks ,
1143
+ } ,
1144
+ } )
1145
+ const registry = new MockRegistry ( {
1146
+ tap : t ,
1147
+ registry : alternateRegistry ,
1148
+ authorization : 'test-other-token' ,
1149
+ } )
1150
+ registry . nock . put ( `/${ pkg } ` , body => {
1151
+ return t . match ( body , putPackagePayload ( {
1152
+ pkg, alternateRegistry, version,
1153
+ } ) )
1154
+ } ) . reply ( 200 , { } )
1155
+ await npm . exec ( 'publish' , [ ] )
1156
+ } )
1157
+
1158
+ t . test ( 'ALLOWS publish when latest dist-tag is missing from response' , async t => {
1159
+ const version = '100.0.0'
1160
+
1161
+ const { npm } = await loadMockNpm ( t , {
1162
+ config : {
1163
+ loglevel : 'silent' ,
1164
+ [ `${ alternateRegistry . slice ( 6 ) } /:_authToken` ] : 'test-other-token' ,
1165
+ } ,
1166
+ prefixDir : {
1167
+ 'package.json' : JSON . stringify ( {
1168
+ ...pkgJson ,
1169
+ version,
1170
+ publishConfig : { registry : alternateRegistry } ,
1171
+ } , null , 2 ) ,
1172
+ } ,
1173
+ mocks : {
1174
+ ...mockNpmRegistryFetch ( {
1175
+ [ `/-/package/${ pkg } /dist-tags` ] : { } ,
1176
+ } ) . mocks ,
1177
+ } ,
1178
+ } )
1179
+ const registry = new MockRegistry ( {
1180
+ tap : t ,
1181
+ registry : alternateRegistry ,
1182
+ authorization : 'test-other-token' ,
1183
+ } )
1184
+ registry . nock . put ( `/${ pkg } ` , body => {
1185
+ return t . match ( body , putPackagePayload ( {
1186
+ pkg, alternateRegistry, version,
1187
+ } ) )
1188
+ } ) . reply ( 200 , { } )
1189
+ await npm . exec ( 'publish' , [ ] )
1190
+ } )
0 commit comments