-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathsupply_test.go
More file actions
1521 lines (1289 loc) · 49.3 KB
/
supply_test.go
File metadata and controls
1521 lines (1289 loc) · 49.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package supply_test
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/cloudfoundry/libbuildpack"
"github.com/cloudfoundry/libbuildpack/ansicleaner"
"github.com/cloudfoundry/nodejs-buildpack/src/nodejs/supply"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
//go:generate mockgen -source=supply.go --destination=mocks_test.go --package=supply_test
var _ = Describe("Supply", func() {
var (
err error
buildDir string
cacheDir string
depsDir string
depsIdx string
depDir string
supplier *supply.Supplier
logger *libbuildpack.Logger
buffer *bytes.Buffer
mockCtrl *gomock.Controller
mockYarn *MockYarn
mockNPM *MockNPM
mockManifest *MockManifest
mockInstaller *MockInstaller
mockCommand *MockCommand
installNode func(libbuildpack.Dependency, string)
installOnlyYarn func(string, string)
)
BeforeEach(func() {
depsDir, err = os.MkdirTemp("", "nodejs-buildpack.deps.")
Expect(err).To(BeNil())
cacheDir, err = os.MkdirTemp("", "nodejs-buildpack.cache.")
Expect(err).To(BeNil())
buildDir, err = os.MkdirTemp("", "nodejs-buildpack.build.")
Expect(err).To(BeNil())
depsIdx = "14"
depDir = filepath.Join(depsDir, depsIdx)
err = os.MkdirAll(depDir, 0755)
Expect(err).To(BeNil())
buffer = new(bytes.Buffer)
logger = libbuildpack.NewLogger(ansicleaner.New(buffer))
mockCtrl = gomock.NewController(GinkgoT())
mockManifest = NewMockManifest(mockCtrl)
mockInstaller = NewMockInstaller(mockCtrl)
mockCommand = NewMockCommand(mockCtrl)
mockYarn = NewMockYarn(mockCtrl)
mockNPM = NewMockNPM(mockCtrl)
installNode = func(dep libbuildpack.Dependency, nodeDir string) {
err := os.MkdirAll(filepath.Join(nodeDir, "bin"), 0755)
Expect(err).To(BeNil())
err = os.WriteFile(filepath.Join(nodeDir, "bin", "node"), []byte("node exe"), 0644)
Expect(err).To(BeNil())
err = os.WriteFile(filepath.Join(nodeDir, "bin", "npm"), []byte("npm exe"), 0644)
Expect(err).To(BeNil())
}
installOnlyYarn = func(_ string, yarnDir string) {
err := os.MkdirAll(filepath.Join(yarnDir, "bin"), 0755)
Expect(err).To(BeNil())
err = os.WriteFile(filepath.Join(yarnDir, "bin", "yarn"), []byte("yarn exe"), 0644)
Expect(err).To(BeNil())
err = os.WriteFile(filepath.Join(yarnDir, "bin", "yarnpkg"), []byte("yarnpkg exe"), 0644)
Expect(err).To(BeNil())
}
args := []string{buildDir, cacheDir, depsDir, depsIdx}
stager := libbuildpack.NewStager(args, logger, &libbuildpack.Manifest{})
supplier = &supply.Supplier{
Stager: stager,
Yarn: mockYarn,
NPM: mockNPM,
Log: logger,
Manifest: mockManifest,
Installer: mockInstaller,
Command: mockCommand,
}
})
AfterEach(func() {
mockCtrl.Finish()
err = os.RemoveAll(depsDir)
Expect(err).To(BeNil())
err = os.RemoveAll(buildDir)
Expect(err).To(BeNil())
})
Describe("LoadPackageJSON", func() {
var packageJSON string
JustBeforeEach(func() {
if packageJSON != "" {
os.WriteFile(filepath.Join(buildDir, "package.json"), []byte(packageJSON), 0644)
}
})
Context("File is invalid JSON", func() {
BeforeEach(func() {
packageJSON = `not actually JSON`
})
It("returns an error", func() {
err = supplier.LoadPackageJSON()
Expect(err).NotTo(BeNil())
})
})
Context("File is valid JSON", func() {
Context("has an engines section", func() {
BeforeEach(func() {
packageJSON = `
{
"name": "node",
"version": "1.0.0",
"main": "server.js",
"author": "CF Buildpacks Team",
"dependencies": {
"logfmt": "~1.1.2",
"express": "~4.0.0"
},
"engines" : {
"yarn" : "*",
"npm" : "npm-x",
"node" : "node-y",
"something" : "3.2.1"
}
}
`
})
It("loads the engines into the supplier", func() {
err = supplier.LoadPackageJSON()
Expect(err).To(BeNil())
Expect(supplier.PackageJSONNodeVersion).To(Equal("node-y"))
Expect(supplier.YarnVersion).To(Equal("*"))
Expect(supplier.NPMVersion).To(Equal("npm-x"))
})
It("logs the node and npm versions", func() {
err = supplier.LoadPackageJSON()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("engines.node (package.json): node-y"))
Expect(buffer.String()).To(ContainSubstring("engines.npm (package.json): npm-x"))
})
Context("the engines section contains iojs", func() {
BeforeEach(func() {
packageJSON = `
{
"engines" : {
"iojs" : "*"
}
}
`
})
It("returns an error", func() {
err = supplier.LoadPackageJSON()
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("io.js not supported by this buildpack"))
})
})
})
Context("does not have an engines section", func() {
BeforeEach(func() {
packageJSON = `
{
"name": "node",
"version": "1.0.0",
"main": "server.js",
"author": "CF Buildpacks Team",
"dependencies": {
"logfmt": "~1.1.2",
"express": "~4.0.0"
}
}
`
})
It("loads the engine struct with empty strings", func() {
err = supplier.LoadPackageJSON()
Expect(err).To(BeNil())
Expect(supplier.NodeVersion).To(Equal(""))
Expect(supplier.YarnVersion).To(Equal(""))
Expect(supplier.NPMVersion).To(Equal(""))
})
It("logs that node and npm are not set", func() {
err = supplier.LoadPackageJSON()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("engines.node (package.json): unspecified"))
Expect(buffer.String()).To(ContainSubstring("engines.npm (package.json): unspecified (use default)"))
})
})
Context("package.json does not exist", func() {
BeforeEach(func() {
packageJSON = ""
})
It("loads the engine struct with empty strings", func() {
err = supplier.LoadPackageJSON()
Expect(err).To(BeNil())
Expect(supplier.NodeVersion).To(Equal(""))
Expect(supplier.YarnVersion).To(Equal(""))
Expect(supplier.NPMVersion).To(Equal(""))
})
It("logs that node and npm are not set", func() {
err = supplier.LoadPackageJSON()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("engines.node (package.json): unspecified"))
Expect(buffer.String()).To(ContainSubstring("engines.npm (package.json): unspecified (use default)"))
})
})
})
})
Describe("Load .nvmrc contents", func() {
Context("digits", func() {
It("will trim and transform nvmrc to appropriate semver for Masterminds semver library", func() {
nvmrcFile := filepath.Join(buildDir, ".nvmrc")
defer os.Remove(nvmrcFile)
testCases := [][]string{
{"10", "10.*.*"},
{"10.2", "10.2.*"},
{"v10", "10.*.*"},
{"10.2.3", "10.2.3"},
{"v10.2.3", "10.2.3"},
}
for _, testCase := range testCases {
Expect(os.WriteFile(nvmrcFile, []byte(testCase[0]), 0777)).To(Succeed())
Expect(supplier.LoadNvmrc()).To(Succeed())
Expect(supplier.NvmrcNodeVersion).To(Equal(testCase[1]), fmt.Sprintf("failed for test case %s : %s", testCase[0], testCase[1]))
}
})
})
Context("lts/something", func() {
It("will read and trim lts versions to appropriate semver for Masterminds semver library", func() {
nvmrcFile := filepath.Join(buildDir, ".nvmrc")
defer os.Remove(nvmrcFile)
testCases := [][]string{
{"lts/hydrogen", "18.*.*"},
{"lts/*", "18.*.*"},
}
for _, testCase := range testCases {
Expect(os.WriteFile(nvmrcFile, []byte(testCase[0]), 0777)).To(Succeed())
Expect(supplier.LoadNvmrc()).To(Succeed())
Expect(supplier.NvmrcNodeVersion).To(Equal(testCase[1]), fmt.Sprintf("failed for test case %s : %s", testCase[0], testCase[1]))
}
})
})
Context("node", func() {
It("should read and trim lts versions", func() {
nvmrcFile := filepath.Join(buildDir, ".nvmrc")
defer os.Remove(nvmrcFile)
Expect(os.WriteFile(nvmrcFile, []byte("node"), 0777)).To(Succeed())
Expect(supplier.LoadNvmrc()).To(Succeed())
Expect(supplier.NvmrcNodeVersion).To(Equal("*"), fmt.Sprintf("failed for test case %s : %s", "node", "*"))
})
})
})
Describe("WarnNodeEngine", func() {
Context("node version not specified", func() {
It("warns that nvmrc version will be ignored in favor of package.json", func() {
supplier.NvmrcNodeVersion = "13.*.*"
supplier.PackageJSONNodeVersion = "13.*.*"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(BeEmpty())
})
It("warns that different nvmrc version will be ignored in favor of package.json", func() {
supplier.NvmrcNodeVersion = "13.*.*"
supplier.PackageJSONNodeVersion = "14.*.*"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(ContainSubstring("**WARNING** Node version in .nvmrc ignored in favor of 'engines' field in package.json"))
})
It("warns that node version hasn't been set", func() {
supplier.NvmrcNodeVersion = ""
supplier.PackageJSONNodeVersion = ""
supplier.WarnNodeEngine()
Expect(buffer.String()).To(ContainSubstring("**WARNING** Node version not specified in package.json or .nvmrc. See: http://docs.cloudfoundry.org/buildpacks/node/node-tips.html"))
})
})
Context("node version is set to node in nvmrc", func() {
It("warns that latest node version is being used", func() {
supplier.NvmrcNodeVersion = "node"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(ContainSubstring("**WARNING** .nvmrc specified latest node version, this will be selected from versions available in manifest.yml"))
Expect(buffer.String()).To(ContainSubstring("**WARNING** Using the node version specified in your .nvmrc See: http://docs.cloudfoundry.org/buildpacks/node/node-tips.html"))
})
})
Context("node version is set to lts in nvmrc", func() {
It("warns that latest lts version is being used", func() {
supplier.NvmrcNodeVersion = "lts/*"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(ContainSubstring("**WARNING** .nvmrc specified an lts version, this will be selected from versions available in manifest.yml"))
})
})
Context("node version is *", func() {
It("warns that the node semver is dangerous", func() {
supplier.PackageJSONNodeVersion = "*"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(ContainSubstring("**WARNING** Dangerous semver range (*) in engines.node. See: http://docs.cloudfoundry.org/buildpacks/node/node-tips.html"))
})
})
Context("node version is >x", func() {
It("warns that the node semver is dangerous", func() {
supplier.PackageJSONNodeVersion = ">5"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(ContainSubstring("**WARNING** Dangerous semver range (>) in engines.node. See: http://docs.cloudfoundry.org/buildpacks/node/node-tips.html"))
})
})
Context("node version is 'safe' semver", func() {
It("does not log anything", func() {
supplier.PackageJSONNodeVersion = "~>10"
supplier.WarnNodeEngine()
Expect(buffer.String()).To(Equal(""))
})
})
})
Describe("When nvmrc is present", func() {
var (
dep libbuildpack.Dependency
versions []string
)
BeforeEach(func() {
dep = libbuildpack.Dependency{Name: "node", Version: "6.10.2"}
mockManifest.EXPECT().DefaultVersion("node").Return(dep, nil).AnyTimes()
versions = []string{
"4.0.0", "4.0.1", "4.2.3",
"6.0.0", "6.0.2", "6.2.3",
"8.0.1", "8.0.3", "8.2.3",
"10.0.0", "10.0.4", "10.2.3",
"11.0.0", "11.0.5", "11.2.3",
}
mockManifest.EXPECT().AllDependencyVersions("node").Return(versions).AnyTimes()
})
Context("nvmrc is present and engines field in package.json is present", func() {
It("selects the version from the engines field in packages.json", func() {
supplier.PackageJSONNodeVersion = "10.0.0"
supplier.NvmrcNodeVersion = "10.2.3"
Expect(supplier.ChooseNodeVersion()).To(Succeed())
Expect(supplier.NodeVersion).To(Equal("10.0.0"))
})
})
Context("nvmrc is present and engines field in package.json is missing", func() {
It("selects the version in nvmrc", func() {
supplier.PackageJSONNodeVersion = ""
supplier.NvmrcNodeVersion = "10.2.3"
Expect(supplier.ChooseNodeVersion()).To(Succeed())
Expect(supplier.NodeVersion).To(Equal("10.2.3"))
})
})
Context("nvmrc is missing and engines field in package.json is present", func() {
It("selects version from engines in package.json", func() {
supplier.PackageJSONNodeVersion = "11.2.3"
supplier.NvmrcNodeVersion = ""
Expect(supplier.ChooseNodeVersion()).To(Succeed())
Expect(supplier.NodeVersion).To(Equal("11.2.3"))
})
})
Context("package.json engines field and nvmrc are both specified", func() {
It("selects version from package.json engines field", func() {
supplier.NvmrcNodeVersion = "8.*.*"
supplier.PackageJSONNodeVersion = ">8.0.3"
err := supplier.ChooseNodeVersion()
Expect(err).ToNot(HaveOccurred())
Expect(supplier.NodeVersion).To(Equal("11.2.3"))
})
})
})
Describe(".nvmrc validation", func() {
AfterEach(func() {
Expect(os.Remove(filepath.Join(buildDir, ".nvmrc"))).To(Succeed())
})
Context("given valid .nvmrc", func() {
It("validate should succeed", func() {
validVersions := []string{"11.4", "node", "lts/*", "lts/hydrogen", "10", "10.1.1"}
for _, version := range validVersions {
Expect(os.WriteFile(filepath.Join(buildDir, ".nvmrc"), []byte(version), 0777)).To(Succeed())
Expect(supplier.LoadNvmrc()).To(Succeed())
}
})
})
Context("given an invalid .nvmrc", func() {
It("validate should be fail", func() {
invalidVersions := []string{"11.4.x", "invalid", "~1.1.2", ">11.0", "< 11.4.2", "^1.2.3", "11.*.*", "10.1.x", "10.1.X", "lts/invalidname"}
for _, version := range invalidVersions {
Expect(os.WriteFile(filepath.Join(buildDir, ".nvmrc"), []byte(version), 0777)).To(Succeed())
Expect(supplier.LoadNvmrc()).ToNot(Succeed())
}
})
})
})
Describe("InstallNode", func() {
var nodeDir string
BeforeEach(func() {
nodeDir = filepath.Join(depDir, "node")
})
AfterEach(func() {
Expect(os.RemoveAll(nodeDir)).To(Succeed())
})
Context("node version use semver", func() {
BeforeEach(func() {
versions := []string{"6.10.2", "6.11.1", "4.8.2", "4.8.3", "7.0.0"}
mockManifest.EXPECT().AllDependencyVersions("node").Return(versions)
mockManifest.EXPECT().DefaultVersion("node").Return(libbuildpack.Dependency{"node", "0.0.0"}, nil).AnyTimes()
})
It("installs the correct version from the manifest", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "4.8.3"}
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.PackageJSONNodeVersion = "~>4"
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
})
It("handles '>=6.11.1 <7.0'", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "6.11.1"}
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.PackageJSONNodeVersion = ">=6.11.1 <7.0.0"
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
})
It("handles '>=6.11.1, <7.0'", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "6.11.1"}
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.PackageJSONNodeVersion = ">=6.11.1, <7.0"
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
})
It("creates a symlink in <depDir>/bin", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "6.10.2"}
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.PackageJSONNodeVersion = "6.10.*"
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
link, err := os.Readlink(filepath.Join(depsDir, depsIdx, "bin", "node"))
Expect(err).To(BeNil())
Expect(link).To(Equal("../node/bin/node"))
link, err = os.Readlink(filepath.Join(depsDir, depsIdx, "bin", "npm"))
Expect(err).To(BeNil())
Expect(link).To(Equal("../node/bin/npm"))
})
})
Context("node version is unset", func() {
It("installs the default version from the manifest", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "6.10.2"}
mockManifest.EXPECT().DefaultVersion("node").Return(dep, nil)
mockManifest.EXPECT().AllDependencyVersions(gomock.Any())
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.NodeVersion = ""
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
})
})
Context("Installing Node >=18", func() {
It("SSL_CERT_DIR env variable is set", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "18.0.0"}
mockManifest.EXPECT().DefaultVersion("node").Return(dep, nil)
mockManifest.EXPECT().AllDependencyVersions(gomock.Any())
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.NodeVersion = ""
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
_, SSLEnvironmentVariable := os.LookupEnv("SSL_CERT_DIR")
Expect(SSLEnvironmentVariable).To(BeTrue())
os.Unsetenv("SSL_CERT_DIR")
})
})
Context("Installing Node <18", func() {
It("SSL_CERT_DIR env variable is not set", func() {
dep := libbuildpack.Dependency{Name: "node", Version: "16.0.0"}
mockManifest.EXPECT().DefaultVersion("node").Return(dep, nil)
mockManifest.EXPECT().AllDependencyVersions(gomock.Any())
mockInstaller.EXPECT().InstallDependency(dep, nodeDir).Do(installNode).Return(nil)
supplier.NodeVersion = ""
err = supplier.ChooseNodeVersion()
Expect(err).To(BeNil())
err = supplier.InstallNode()
Expect(err).To(BeNil())
_, SSLEnvironmentVariable := os.LookupEnv("SSL_CERT_DIR")
Expect(SSLEnvironmentVariable).To(BeFalse())
})
})
})
Describe("InstallYarn", func() {
var yarnInstallDir string
BeforeEach(func() {
yarnInstallDir = filepath.Join(depsDir, depsIdx, "yarn")
})
Context("yarn version is unset", func() {
BeforeEach(func() {
mockInstaller.EXPECT().InstallOnlyVersion("yarn", yarnInstallDir).Do(installOnlyYarn).Return(nil)
mockCommand.EXPECT().Execute(buildDir, gomock.Any(), gomock.Any(), "yarn", "--version").Do(func(_ string, buffer io.Writer, _ io.Writer, _ string, _ string) {
buffer.Write([]byte("0.32.5\n"))
}).Return(nil)
})
It("installs the only version in the manifest", func() {
supplier.YarnVersion = ""
err = supplier.InstallYarn()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("Installed yarn 0.32.5"))
})
It("creates a symlink in <depDir>/bin", func() {
supplier.YarnVersion = ""
err = supplier.InstallYarn()
Expect(err).To(BeNil())
link, err := os.Readlink(filepath.Join(depsDir, depsIdx, "bin", "yarn"))
Expect(err).To(BeNil())
Expect(link).To(Equal("../yarn/bin/yarn"))
link, err = os.Readlink(filepath.Join(depsDir, depsIdx, "bin", "yarnpkg"))
Expect(err).To(BeNil())
Expect(link).To(Equal("../yarn/bin/yarnpkg"))
})
})
Context("requested yarn version is in manifest", func() {
BeforeEach(func() {
versions := []string{"0.32.5"}
mockManifest.EXPECT().AllDependencyVersions("yarn").Return(versions)
mockInstaller.EXPECT().InstallOnlyVersion("yarn", yarnInstallDir).Do(installOnlyYarn).Return(nil)
mockCommand.EXPECT().Execute(buildDir, gomock.Any(), gomock.Any(), "yarn", "--version").Do(func(_ string, buffer io.Writer, _ io.Writer, _ string, _ string) {
buffer.Write([]byte("0.32.5\n"))
}).Return(nil)
})
It("installs the correct version from the manifest", func() {
supplier.YarnVersion = "0.32.x"
err = supplier.InstallYarn()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("Installed yarn 0.32.5"))
})
})
Context("requested yarn version is not in manifest", func() {
BeforeEach(func() {
versions := []string{"0.32.5"}
mockManifest.EXPECT().AllDependencyVersions("yarn").Return(versions)
})
It("returns an error", func() {
supplier.YarnVersion = "1.0.x"
err = supplier.InstallYarn()
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(Equal("package.json requested 1.0.x, buildpack only includes yarn version 0.32.5"))
})
})
})
Describe("InstallNPM", func() {
BeforeEach(func() {
mockCommand.EXPECT().Execute(buildDir, gomock.Any(), gomock.Any(), "npm", "--version").Do(func(_ string, buffer io.Writer, _ io.Writer, _ string, _ string) {
buffer.Write([]byte("1.2.3\n"))
}).Return(nil)
})
Context("npm version is not set", func() {
It("uses the version of npm packaged with node", func() {
err = supplier.InstallNPM()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("Using default npm version: 1.2.3"))
})
})
Context("npm version is set", func() {
Context("requested version is already installed", func() {
It("Uses the version of npm packaged with node", func() {
supplier.NPMVersion = "1.2.3"
err = supplier.InstallNPM()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("npm 1.2.3 already installed with node"))
})
})
Context("requested version has minor .x and is already installed", func() {
It("Uses the version of npm packaged with node", func() {
supplier.NPMVersion = "1.2.x"
err = supplier.InstallNPM()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("npm 1.2.3 already installed with node"))
})
})
It("installs the requested npm version using packaged npm", func() {
mockCommand.EXPECT().Execute(buildDir, gomock.Any(), gomock.Any(),
"npm", "install", "--unsafe-perm", "--quiet", "-g", "npm@4.5.6",
"--userconfig", filepath.Join(buildDir, ".npmrc")).Return(nil)
supplier.NPMVersion = "4.5.6"
err = supplier.InstallNPM()
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("Downloading and installing npm 4.5.6 (replacing version 1.2.3)..."))
})
})
})
Describe("ReadPackageJSON", func() {
Context("package.json has prebuild script", func() {
BeforeEach(func() {
packageJSON := `
{
"scripts" : {
"script": "script",
"heroku-prebuild": "makestuff",
"thing": "thing"
}
}
`
Expect(os.WriteFile(filepath.Join(buildDir, "package.json"), []byte(packageJSON), 0644)).To(Succeed())
})
It("sets PreBuild", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.PreBuild).To(Equal("makestuff"))
})
})
Context("package.json has postbuild script", func() {
BeforeEach(func() {
packageJSON := `
{
"scripts" : {
"script": "script",
"heroku-postbuild": "logstuff",
"thing": "thing"
}
}
`
Expect(os.WriteFile(filepath.Join(buildDir, "package.json"), []byte(packageJSON), 0644)).To(Succeed())
})
It("sets PostBuild", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.PostBuild).To(Equal("logstuff"))
})
})
Context("package.json has start script", func() {
BeforeEach(func() {
packageJSON := `
{
"scripts" : {
"script": "script",
"start": "start-my-app",
"thing": "thing"
}
}
`
Expect(os.WriteFile(filepath.Join(buildDir, "package.json"), []byte(packageJSON), 0644)).To(Succeed())
})
It("sets StartScript", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.StartScript).To(Equal("start-my-app"))
})
})
Context("package.json does not exist", func() {
It("warns user", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("**WARNING** No package.json found"))
})
})
Context("yarn.lock exists", func() {
BeforeEach(func() {
Expect(os.WriteFile(filepath.Join(buildDir, "yarn.lock"), []byte("{}"), 0644)).To(Succeed())
})
It("sets UseYarn to true", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.UseYarn).To(BeTrue())
})
})
Context("yarn.lock does not exist", func() {
It("sets UseYarn to false", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.UseYarn).To(BeFalse())
})
})
Context("node_modules exists", func() {
BeforeEach(func() {
Expect(os.MkdirAll(filepath.Join(buildDir, "node_modules"), 0755)).To(Succeed())
})
It("sets NPMRebuild to true", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.IsVendored).To(BeTrue())
})
})
Context("node_modules does not exist", func() {
It("sets NPMRebuild to false", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.IsVendored).To(BeFalse())
})
})
Context("dev dependencies exist", func() {
BeforeEach(func() {
packageJSON := `
{
"devDependencies": {
"logger": "^0.0.1"
}
}
`
Expect(os.WriteFile(filepath.Join(buildDir, "package.json"), []byte(packageJSON), 0644)).To(Succeed())
})
It("sets HasDevDependencies to true", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.HasDevDependencies).To(BeTrue())
})
})
Context("dev dependencies do not exist", func() {
It("sets HasDevDependencies to false", func() {
Expect(supplier.ReadPackageJSON()).To(Succeed())
Expect(supplier.HasDevDependencies).To(BeFalse())
})
})
})
Describe("TipVendorDependencies", func() {
Context("node_modules exists and has subdirectories", func() {
BeforeEach(func() {
Expect(os.MkdirAll(filepath.Join(buildDir, "node_modules", "exciting_module"), 0755)).To(BeNil())
})
It("does not log anything", func() {
Expect(supplier.TipVendorDependencies()).To(BeNil())
Expect(buffer.String()).To(Equal(""))
})
})
Context("node_modules exists and has NO subdirectories", func() {
BeforeEach(func() {
Expect(os.MkdirAll(filepath.Join(buildDir, "node_modules"), 0755)).To(BeNil())
Expect(os.WriteFile(filepath.Join(buildDir, "node_modules", "a_file"), []byte("content"), 0644)).To(BeNil())
})
It("logs a pro tip", func() {
Expect(supplier.TipVendorDependencies()).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("PRO TIP: It is recommended to vendor the application's Node.js dependencies"))
Expect(buffer.String()).To(ContainSubstring("http://docs.cloudfoundry.org/buildpacks/node/index.html#vendoring"))
})
})
Context("node_modules does not exist", func() {
It("logs a pro tip", func() {
Expect(supplier.TipVendorDependencies()).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("PRO TIP: It is recommended to vendor the application's Node.js dependencies"))
Expect(buffer.String()).To(ContainSubstring("http://docs.cloudfoundry.org/buildpacks/node/index.html#vendoring"))
})
})
})
Describe("ListNodeConfig", func() {
DescribeTable("outputs relevant env vars",
func(key string, value string, expected string) {
supplier.ListNodeConfig([]string{fmt.Sprintf("%s=%s", key, value)})
Expect(buffer.String()).To(Equal(expected))
},
Entry("NPM_CONFIG_", "NPM_CONFIG_THING", "someval", " NPM_CONFIG_THING=someval\n"),
Entry("YARN_", "YARN_KEY", "aval", " YARN_KEY=aval\n"),
Entry("NODE_", "NODE_EXCITING", "newval", " NODE_EXCITING=newval\n"),
Entry("NOT_RELEVANT", "NOT_RELEVANT", "anything", ""),
)
It("warns about NODE_ENV override", func() {
supplier.ListNodeConfig([]string{"NPM_CONFIG_PRODUCTION=true", "NODE_ENV=development"})
Expect(buffer.String()).To(ContainSubstring("npm scripts will see NODE_ENV=production (not 'development')"))
Expect(buffer.String()).To(ContainSubstring("https://docs.npmjs.com/misc/config#production"))
})
})
Describe("WarnUntrackedDependencies", func() {
var (
logfile *os.File
contents string
)
JustBeforeEach(func() {
logfile, err = os.CreateTemp("", "nodejs-buildpack.log")
Expect(err).To(BeNil())
_, err = logfile.Write([]byte(contents))
Expect(err).To(BeNil())
Expect(logfile.Sync()).To(Succeed())
supplier.Logfile = logfile
Expect(supplier.WarnUntrackedDependencies()).To(Succeed())
})
AfterEach(func() {
Expect(logfile.Close()).To(Succeed())
Expect(os.Remove(logfile.Name())).To(Succeed())
})
Context("gulp not found", func() {
BeforeEach(func() {
contents = "stuff\ngulp: not found\nstuff\n"
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("Gulp may not be tracked in package.json"))
})
})
Context("gulp command not found", func() {
BeforeEach(func() {
contents = "stuff\ngulp: command not found\nstuff\n"
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("Gulp may not be tracked in package.json"))
})
})
Context("bower not found", func() {
BeforeEach(func() {
contents = "stuff\nbower: not found\nstuff\n"
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("Bower may not be tracked in package.json"))
})
})
Context("bower command not found", func() {
BeforeEach(func() {
contents = "stuff\nbower: command not found\nstuff\n"
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("Bower may not be tracked in package.json"))
})
})
Context("grunt not found", func() {
BeforeEach(func() {
contents = "stuff\ngrunt: not found\nstuff\n"
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("Grunt may not be tracked in package.json"))
})
})
Context("grunt command not found", func() {
BeforeEach(func() {
contents = "stuff\ngrunt: command not found\nstuff\n"
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("Grunt may not be tracked in package.json"))
})
})
Context("no 'not found' errors", func() {
BeforeEach(func() {
contents = "stuff\ngood command\nstuff\n"
})
It("does not warn the user", func() {
Expect(buffer.String()).To(BeEmpty())
})
})
})
Describe("WarnMissingDevDeps", func() {
var (
logfile *os.File
contents string
)
JustBeforeEach(func() {
logfile, err = os.CreateTemp("", "nodejs-buildpack.log")
Expect(err).To(BeNil())
_, err = logfile.Write([]byte(contents))
Expect(err).To(BeNil())
Expect(logfile.Sync()).To(Succeed())
supplier.Logfile = logfile
Expect(supplier.WarnMissingDevDeps()).To(Succeed())
})
AfterEach(func() {