|
| 1 | +import { expect } from '@oclif/test'; |
| 2 | +import { DependencyGraphBuilder } from './dependency-graph-builder'; |
| 3 | +import { Resource } from './entities/resource'; |
| 4 | +import { CompiledProject } from './entities/compiled-project'; |
| 5 | +import { ProjectConfig } from '../parser/entities/configs/project'; |
| 6 | + |
| 7 | +describe('Dependency graph tests', () => { |
| 8 | + |
| 9 | + it('parses and replace resource references', () => { |
| 10 | + const resource1 = () => new Resource( |
| 11 | + 'homebrew_installation', |
| 12 | + undefined, |
| 13 | + new Map([ |
| 14 | + ['directory', '/usr/opt'] |
| 15 | + ]), |
| 16 | + '', |
| 17 | + ); |
| 18 | + |
| 19 | + const resource2 = () => new Resource( |
| 20 | + 'homebrew_options', |
| 21 | + undefined, |
| 22 | + new Map([ |
| 23 | + ['directory', '${homebrew_installation.directory}'] |
| 24 | + ]), |
| 25 | + '', |
| 26 | + ); |
| 27 | + |
| 28 | + const graph = new Map([resource1(), resource2()].map((g) => [g.id, g])); |
| 29 | + const project = new CompiledProject({ applyableGraph: graph, projectConfig: {} as ProjectConfig }); |
| 30 | + |
| 31 | + expect(() => DependencyGraphBuilder.buildDependencyGraph(project)).to.not.throw() |
| 32 | + expect(project.applyableGraph.get('homebrew_installation')).to.deep.eq(resource1()) |
| 33 | + expect(project.applyableGraph.get('homebrew_options')).to.not.deep.eq(resource2()) |
| 34 | + expect(project.applyableGraph.get('homebrew_options').parameters.get('directory')).to.eq(graph.get('homebrew_installation')!.parameters.get('directory')); |
| 35 | + }) |
| 36 | + |
| 37 | + it('validates invalid resources', () => { |
| 38 | + const resource1 = () => new Resource( |
| 39 | + 'homebrew_installation', |
| 40 | + undefined, |
| 41 | + new Map([ |
| 42 | + ['directory', '/usr/opt'] |
| 43 | + ]), |
| 44 | + '', |
| 45 | + ); |
| 46 | + |
| 47 | + const resource2 = () => new Resource( |
| 48 | + 'homebrew_options', |
| 49 | + undefined, |
| 50 | + new Map([ |
| 51 | + ['directory', '${homebrew_installation_invalid.directory}'] |
| 52 | + ]), |
| 53 | + '', |
| 54 | + ); |
| 55 | + |
| 56 | + const graph = new Map([resource1(), resource2()].map((g) => [g.id, g])); |
| 57 | + const project = new CompiledProject({ applyableGraph: graph, projectConfig: {} as ProjectConfig }); |
| 58 | + |
| 59 | + expect(() => DependencyGraphBuilder.buildDependencyGraph(project)).to.throw() |
| 60 | + }) |
| 61 | + |
| 62 | + it('validates invalid parameters', () => { |
| 63 | + const resource1 = () => new Resource( |
| 64 | + 'homebrew_installation', |
| 65 | + undefined, |
| 66 | + new Map([ |
| 67 | + ['directory', '/usr/opt'] |
| 68 | + ]), |
| 69 | + '', |
| 70 | + ); |
| 71 | + |
| 72 | + const resource2 = () => new Resource( |
| 73 | + 'homebrew_options', |
| 74 | + undefined, |
| 75 | + new Map([ |
| 76 | + ['directory', '${homebrew_installation.directory_invalid}'] |
| 77 | + ]), |
| 78 | + '', |
| 79 | + ); |
| 80 | + |
| 81 | + const graph = new Map([resource1(), resource2()].map((g) => [g.id, g])); |
| 82 | + const project = new CompiledProject({ applyableGraph: graph, projectConfig: {} as ProjectConfig }); |
| 83 | + |
| 84 | + expect(() => DependencyGraphBuilder.buildDependencyGraph(project)).to.throw() |
| 85 | + }) |
| 86 | + |
| 87 | + it('handles multiple resource references', () => { |
| 88 | + const resource1 = () => new Resource( |
| 89 | + 'homebrew_installation', |
| 90 | + undefined, |
| 91 | + new Map([ |
| 92 | + ['directory', '/usr/opt'] |
| 93 | + ]), |
| 94 | + '', |
| 95 | + ); |
| 96 | + |
| 97 | + const resource2 = () => new Resource( |
| 98 | + 'homebrew_options', |
| 99 | + undefined, |
| 100 | + new Map([ |
| 101 | + ['directory', '$\{homebrew_installation.directory} and $\{homebrew_installation.directory}'] |
| 102 | + ]), |
| 103 | + '', |
| 104 | + ); |
| 105 | + |
| 106 | + const graph = new Map([resource1(), resource2()].map((g) => [g.id, g])); |
| 107 | + const project = new CompiledProject({ applyableGraph: graph, projectConfig: {} as ProjectConfig }); |
| 108 | + |
| 109 | + expect(() => DependencyGraphBuilder.buildDependencyGraph(project)).to.not.throw() |
| 110 | + expect(project.applyableGraph.get('homebrew_installation')).to.deep.eq(resource1()) |
| 111 | + expect(project.applyableGraph.get('homebrew_options')).to.not.deep.eq(resource2()) |
| 112 | + expect(project.applyableGraph.get('homebrew_options').parameters.get('directory')).to.eq(`/usr/opt and /usr/opt`); |
| 113 | + }) |
| 114 | + |
| 115 | + it('handles named resources', () => { |
| 116 | + const resource1 = () => new Resource( |
| 117 | + 'homebrew_installation', |
| 118 | + 'first', |
| 119 | + new Map([ |
| 120 | + ['directory', '/usr/opt'] |
| 121 | + ]), |
| 122 | + '', |
| 123 | + ); |
| 124 | + |
| 125 | + const resource2 = () => new Resource( |
| 126 | + 'homebrew_options', |
| 127 | + undefined, |
| 128 | + new Map([ |
| 129 | + ['directory', '$\{homebrew_installation.first.directory}'] |
| 130 | + ]), |
| 131 | + '', |
| 132 | + ); |
| 133 | + |
| 134 | + const graph = new Map([resource1(), resource2()].map((g) => [g.id, g])); |
| 135 | + const project = new CompiledProject({ applyableGraph: graph, projectConfig: {} as ProjectConfig }); |
| 136 | + |
| 137 | + expect(() => DependencyGraphBuilder.buildDependencyGraph(project)).to.not.throw() |
| 138 | + expect(project.applyableGraph.get('homebrew_installation.first')).to.deep.eq(resource1()) |
| 139 | + expect(project.applyableGraph.get('homebrew_options')).to.not.deep.eq(resource2()) |
| 140 | + expect(project.applyableGraph.get('homebrew_options').parameters.get('directory')).to.eq(`/usr/opt`); |
| 141 | + }) |
| 142 | +}) |
0 commit comments