|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup'; |
| 17 | +import { Config } from '@oclif/core'; |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { Package } from '@salesforce/packaging'; |
| 20 | +import sinon from 'sinon'; |
| 21 | +import { SfCommand } from '@salesforce/sf-plugins-core'; |
| 22 | +import { SfProject } from '@salesforce/core'; |
| 23 | +import { PackageVersionDisplayDependenciesCommand } from '../../../src/commands/package/version/displaydependencies.js'; |
| 24 | + |
| 25 | +const DOT_OUTPUT = `strict digraph G { |
| 26 | + node_04tXXX [label="Pkg@1.0.0.0" color="green"] |
| 27 | + node_04tYYY [label="Dep@2.0.0.1" color="green"] |
| 28 | + node_04tXXX -> node_04tYYY |
| 29 | +}`; |
| 30 | + |
| 31 | +describe('package:version:displaydependencies', () => { |
| 32 | + const $$ = new TestContext(); |
| 33 | + const testOrg = new MockTestOrgData(); |
| 34 | + const config = new Config({ root: import.meta.url }); |
| 35 | + |
| 36 | + let logStub: sinon.SinonStub; |
| 37 | + let getDependencyGraphStub: sinon.SinonStub; |
| 38 | + |
| 39 | + before(async () => { |
| 40 | + await $$.stubAuths(testOrg); |
| 41 | + await config.load(); |
| 42 | + }); |
| 43 | + |
| 44 | + beforeEach(async () => { |
| 45 | + logStub = $$.SANDBOX.stub(SfCommand.prototype, 'log'); |
| 46 | + getDependencyGraphStub = $$.SANDBOX.stub(Package, 'getDependencyGraph').resolves({ |
| 47 | + getDependencyDotProducer: sinon.stub().resolves({ |
| 48 | + produce: sinon.stub().returns(DOT_OUTPUT), |
| 49 | + }), |
| 50 | + } as never); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + $$.restore(); |
| 55 | + $$.SANDBOX.restore(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should call getDependencyGraph with installation key when provided', async () => { |
| 59 | + const command = new PackageVersionDisplayDependenciesCommand( |
| 60 | + ['-p', '04tXXXXXXXXXXXXXX0', '-v', testOrg.username, '-k', 'myKey123'], |
| 61 | + config |
| 62 | + ); |
| 63 | + command.project = SfProject.getInstance(); |
| 64 | + |
| 65 | + await command.run(); |
| 66 | + |
| 67 | + expect(getDependencyGraphStub.calledOnce).to.be.true; |
| 68 | + const callArgs = getDependencyGraphStub.firstCall.args; |
| 69 | + expect(callArgs[0]).to.equal('04tXXXXXXXXXXXXXX0'); |
| 70 | + expect(callArgs[3]).to.deep.include({ installationKey: 'myKey123' }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should call getDependencyGraph without installation key when not provided', async () => { |
| 74 | + const command = new PackageVersionDisplayDependenciesCommand( |
| 75 | + ['-p', '04tXXXXXXXXXXXXXX0', '-v', testOrg.username], |
| 76 | + config |
| 77 | + ); |
| 78 | + command.project = SfProject.getInstance(); |
| 79 | + |
| 80 | + await command.run(); |
| 81 | + |
| 82 | + expect(getDependencyGraphStub.calledOnce).to.be.true; |
| 83 | + const callArgs = getDependencyGraphStub.firstCall.args; |
| 84 | + expect(callArgs[0]).to.equal('04tXXXXXXXXXXXXXX0'); |
| 85 | + expect(callArgs[3]).to.deep.include({ installationKey: undefined }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should output DOT code to stdout when not --json', async () => { |
| 89 | + const command = new PackageVersionDisplayDependenciesCommand( |
| 90 | + ['-p', '04tXXXXXXXXXXXXXX0', '-v', testOrg.username], |
| 91 | + config |
| 92 | + ); |
| 93 | + command.project = SfProject.getInstance(); |
| 94 | + |
| 95 | + await command.run(); |
| 96 | + |
| 97 | + expect(logStub.calledOnce).to.be.true; |
| 98 | + expect(logStub.firstCall.args[0]).to.equal(DOT_OUTPUT); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should return DOT code as result when --json', async () => { |
| 102 | + const command = new PackageVersionDisplayDependenciesCommand( |
| 103 | + ['-p', '04tXXXXXXXXXXXXXX0', '-v', testOrg.username, '--json'], |
| 104 | + config |
| 105 | + ); |
| 106 | + command.project = SfProject.getInstance(); |
| 107 | + |
| 108 | + const result = await command.run(); |
| 109 | + |
| 110 | + expect(result).to.equal(DOT_OUTPUT); |
| 111 | + expect(logStub.called).to.be.false; |
| 112 | + }); |
| 113 | + |
| 114 | + it('should pass edge-direction option to getDependencyGraph', async () => { |
| 115 | + const command = new PackageVersionDisplayDependenciesCommand( |
| 116 | + ['-p', '04tXXXXXXXXXXXXXX0', '-v', testOrg.username, '--edge-direction', 'root-last'], |
| 117 | + config |
| 118 | + ); |
| 119 | + command.project = SfProject.getInstance(); |
| 120 | + |
| 121 | + await command.run(); |
| 122 | + |
| 123 | + const callArgs = getDependencyGraphStub.firstCall.args; |
| 124 | + expect(callArgs[3]).to.deep.include({ edgeDirection: 'root-last' }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments