Skip to content

Commit 271e881

Browse files
authored
Decouple RegionNode from other tree nodes (#797)
* Tree Nodes no longer coupled to RegionNode * Remove RegionNode from ErrorNode tests * Removed unused test classes
1 parent 0acd673 commit 271e881

File tree

9 files changed

+61
-115
lines changed

9 files changed

+61
-115
lines changed

src/awsexplorer/defaultRegionNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export class DefaultRegionNode extends AWSTreeNodeBase implements RegionNode {
3333
this.info = info
3434
this.update(info)
3535

36-
this.cloudFormationNode = new DefaultCloudFormationNode(this)
37-
this.lambdaFunctionGroupNode = new DefaultLambdaFunctionGroupNode(this)
36+
this.cloudFormationNode = new DefaultCloudFormationNode(this.regionCode)
37+
this.lambdaFunctionGroupNode = new DefaultLambdaFunctionGroupNode(this.regionCode)
3838
}
3939

4040
public async getChildren(): Promise<AWSTreeNodeBase[]> {

src/lambda/explorer/cloudFormationNodes.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@ import { CloudFormationClient } from '../../shared/clients/cloudFormationClient'
1313
import { LambdaClient } from '../../shared/clients/lambdaClient'
1414
import { ext } from '../../shared/extensionGlobals'
1515
import { AWSTreeErrorHandlerNode } from '../../shared/treeview/nodes/awsTreeErrorHandlerNode'
16+
import { AWSTreeNodeBase } from '../../shared/treeview/nodes/awsTreeNodeBase'
1617
import { ErrorNode } from '../../shared/treeview/nodes/errorNode'
1718
import { PlaceholderNode } from '../../shared/treeview/nodes/placeholderNode'
18-
import { RegionNode } from '../../shared/treeview/nodes/regionNode'
1919
import { intersection, toArrayAsync, toMap, toMapAsync, updateInPlace } from '../../shared/utilities/collectionUtils'
2020
import { listCloudFormationStacks, listLambdaFunctions } from '../utils'
2121
import { FunctionNodeBase } from './functionNode'
2222

2323
export interface CloudFormationNode extends AWSTreeErrorHandlerNode {
24-
readonly regionCode: string
25-
26-
readonly parent: RegionNode
27-
2824
getChildren(): Thenable<(CloudFormationStackNode | ErrorNode)[]>
2925

3026
updateChildren(): Thenable<void>
@@ -33,11 +29,7 @@ export interface CloudFormationNode extends AWSTreeErrorHandlerNode {
3329
export class DefaultCloudFormationNode extends AWSTreeErrorHandlerNode implements CloudFormationNode {
3430
private readonly stackNodes: Map<string, CloudFormationStackNode>
3531

36-
public get regionCode(): string {
37-
return this.parent.regionCode
38-
}
39-
40-
public constructor(public readonly parent: RegionNode) {
32+
public constructor(private readonly regionCode: string) {
4133
super('CloudFormation', vscode.TreeItemCollapsibleState.Collapsed)
4234
this.stackNodes = new Map<string, CloudFormationStackNode>()
4335
}
@@ -61,7 +53,7 @@ export class DefaultCloudFormationNode extends AWSTreeErrorHandlerNode implement
6153
this.stackNodes,
6254
stacks.keys(),
6355
key => this.stackNodes.get(key)!.update(stacks.get(key)!),
64-
key => new DefaultCloudFormationStackNode(this, stacks.get(key)!)
56+
key => new DefaultCloudFormationStackNode(this, this.regionCode, stacks.get(key)!)
6557
)
6658
}
6759
}
@@ -71,7 +63,7 @@ export interface CloudFormationStackNode extends AWSTreeErrorHandlerNode {
7163
readonly stackId?: CloudFormation.StackId
7264
readonly stackName: CloudFormation.StackName
7365

74-
readonly parent: CloudFormationNode
66+
readonly parent: AWSTreeNodeBase
7567

7668
getChildren(): Thenable<(CloudFormationFunctionNode | PlaceholderNode)[]>
7769

@@ -81,11 +73,11 @@ export interface CloudFormationStackNode extends AWSTreeErrorHandlerNode {
8173
export class DefaultCloudFormationStackNode extends AWSTreeErrorHandlerNode implements CloudFormationStackNode {
8274
private readonly functionNodes: Map<string, CloudFormationFunctionNode>
8375

84-
public get regionCode(): string {
85-
return this.parent.regionCode
86-
}
87-
88-
public constructor(public readonly parent: CloudFormationNode, private stackSummary: CloudFormation.StackSummary) {
76+
public constructor(
77+
public readonly parent: AWSTreeNodeBase,
78+
public readonly regionCode: string,
79+
private stackSummary: CloudFormation.StackSummary
80+
) {
8981
super('', vscode.TreeItemCollapsibleState.Collapsed)
9082

9183
this.update(stackSummary)
@@ -145,7 +137,7 @@ export class DefaultCloudFormationStackNode extends AWSTreeErrorHandlerNode impl
145137
this.functionNodes,
146138
intersection(resources, functions.keys()),
147139
key => this.functionNodes.get(key)!.update(functions.get(key)!),
148-
key => new DefaultCloudFormationFunctionNode(this, functions.get(key)!)
140+
key => new DefaultCloudFormationFunctionNode(this, this.regionCode, functions.get(key)!)
149141
)
150142
}
151143

@@ -168,12 +160,12 @@ export interface CloudFormationFunctionNode extends FunctionNodeBase {
168160
}
169161

170162
export class DefaultCloudFormationFunctionNode extends FunctionNodeBase {
171-
public get regionCode(): string {
172-
return this.parent.regionCode
173-
}
174-
175-
public constructor(public readonly parent: CloudFormationStackNode, configuration: Lambda.FunctionConfiguration) {
176-
super(configuration)
163+
public constructor(
164+
public readonly parent: AWSTreeNodeBase,
165+
public readonly regionCode: string,
166+
configuration: Lambda.FunctionConfiguration
167+
) {
168+
super(parent, configuration)
177169
this.contextValue = 'awsCloudFormationFunctionNode'
178170
}
179171
}

src/lambda/explorer/functionNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { AWSTreeNodeBase } from '../../shared/treeview/nodes/awsTreeNodeBase'
1212
export abstract class FunctionNodeBase extends AWSTreeNodeBase {
1313
public abstract readonly regionCode: string
1414

15-
protected constructor(public configuration: Lambda.FunctionConfiguration) {
15+
protected constructor(public readonly parent: AWSTreeNodeBase, public configuration: Lambda.FunctionConfiguration) {
1616
super('')
1717
this.update(configuration)
1818
this.iconPath = {

src/lambda/explorer/lambdaNodes.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ import * as vscode from 'vscode'
1111
import { LambdaClient } from '../../shared/clients/lambdaClient'
1212
import { ext } from '../../shared/extensionGlobals'
1313
import { AWSTreeErrorHandlerNode } from '../../shared/treeview/nodes/awsTreeErrorHandlerNode'
14+
import { AWSTreeNodeBase } from '../../shared/treeview/nodes/awsTreeNodeBase'
1415
import { ErrorNode } from '../../shared/treeview/nodes/errorNode'
15-
import { RegionNode } from '../../shared/treeview/nodes/regionNode'
1616
import { toArrayAsync, toMap, updateInPlace } from '../../shared/utilities/collectionUtils'
1717
import { listLambdaFunctions } from '../utils'
1818
import { FunctionNodeBase } from './functionNode'
1919

2020
export interface LambdaFunctionGroupNode extends AWSTreeErrorHandlerNode {
21-
readonly regionCode: string
22-
23-
readonly parent: RegionNode
24-
2521
getChildren(): Thenable<(LambdaFunctionNode | ErrorNode)[]>
2622

2723
updateChildren(): Thenable<void>
@@ -30,11 +26,7 @@ export interface LambdaFunctionGroupNode extends AWSTreeErrorHandlerNode {
3026
export class DefaultLambdaFunctionGroupNode extends AWSTreeErrorHandlerNode implements LambdaFunctionGroupNode {
3127
private readonly functionNodes: Map<string, LambdaFunctionNode>
3228

33-
public get regionCode(): string {
34-
return this.parent.regionCode
35-
}
36-
37-
public constructor(public readonly parent: RegionNode) {
29+
public constructor(private readonly regionCode: string) {
3830
super('Lambda', vscode.TreeItemCollapsibleState.Collapsed)
3931
this.functionNodes = new Map<string, LambdaFunctionNode>()
4032
}
@@ -63,22 +55,22 @@ export class DefaultLambdaFunctionGroupNode extends AWSTreeErrorHandlerNode impl
6355
this.functionNodes,
6456
functions.keys(),
6557
key => this.functionNodes.get(key)!.update(functions.get(key)!),
66-
key => new DefaultLambdaFunctionNode(this, functions.get(key)!)
58+
key => new DefaultLambdaFunctionNode(this, this.regionCode, functions.get(key)!)
6759
)
6860
}
6961
}
7062

7163
export interface LambdaFunctionNode extends FunctionNodeBase {
72-
readonly parent: LambdaFunctionGroupNode
64+
readonly parent: AWSTreeNodeBase
7365
}
7466

7567
export class DefaultLambdaFunctionNode extends FunctionNodeBase implements LambdaFunctionNode {
76-
public get regionCode(): string {
77-
return this.parent.regionCode
78-
}
79-
80-
public constructor(public readonly parent: LambdaFunctionGroupNode, configuration: Lambda.FunctionConfiguration) {
81-
super(configuration)
68+
public constructor(
69+
public readonly parent: AWSTreeNodeBase,
70+
public readonly regionCode: string,
71+
configuration: Lambda.FunctionConfiguration
72+
) {
73+
super(parent, configuration)
8274
this.contextValue = 'awsRegionFunctionNode'
8375
}
8476
}

src/test/lambda/explorer/cloudFormationNodes.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import * as assert from 'assert'
77
import { CloudFormation, Lambda } from 'aws-sdk'
88
import * as os from 'os'
9-
import { DefaultRegionNode } from '../../../awsexplorer/defaultRegionNode'
109
import {
1110
CloudFormationStackNode,
1211
DefaultCloudFormationFunctionNode,
@@ -18,10 +17,10 @@ import { EcsClient } from '../../../shared/clients/ecsClient'
1817
import { LambdaClient } from '../../../shared/clients/lambdaClient'
1918
import { StsClient } from '../../../shared/clients/stsClient'
2019
import { ext } from '../../../shared/extensionGlobals'
21-
import { RegionInfo } from '../../../shared/regions/regionInfo'
2220
import { ErrorNode } from '../../../shared/treeview/nodes/errorNode'
2321
import { PlaceholderNode } from '../../../shared/treeview/nodes/placeholderNode'
2422
import { MockCloudFormationClient } from '../../shared/clients/mockClients'
23+
import { TestAWSTreeNode } from '../../shared/treeview/nodes/testAWSTreeNode'
2524
import { clearTestIconPaths, IconPath, setupTestIconPaths } from '../../shared/utilities/iconPathUtils'
2625

2726
async function* asyncGenerator<T>(items: T[]): AsyncIterableIterator<T> {
@@ -201,10 +200,9 @@ describe('DefaultCloudFormationStackNode', () => {
201200
})
202201

203202
function generateTestNode(): CloudFormationStackNode {
204-
return new DefaultCloudFormationStackNode(
205-
new DefaultCloudFormationNode(new DefaultRegionNode(new RegionInfo('code', 'name'))),
206-
fakeStackSummary
207-
)
203+
const parentNode = new TestAWSTreeNode('test node')
204+
205+
return new DefaultCloudFormationStackNode(parentNode, 'someregioncode', fakeStackSummary)
208206
}
209207
})
210208

@@ -253,7 +251,7 @@ describe('DefaultCloudFormationNode', () => {
253251
}
254252
}
255253

256-
const cloudFormationNode = new DefaultCloudFormationNode(new DefaultRegionNode(new RegionInfo('code', 'name')))
254+
const cloudFormationNode = new DefaultCloudFormationNode('someregioncode')
257255

258256
const children = await cloudFormationNode.getChildren()
259257

@@ -290,18 +288,16 @@ describe('DefaultCloudFormationNode', () => {
290288

291289
it('handles error', async () => {
292290
class ThrowErrorDefaultCloudFormationNode extends DefaultCloudFormationNode {
293-
public constructor(public readonly regionNode: DefaultRegionNode) {
294-
super(regionNode)
291+
public constructor() {
292+
super('someregioncode')
295293
}
296294

297295
public async updateChildren(): Promise<void> {
298296
throw new Error('Hello there!')
299297
}
300298
}
301299

302-
const testNode: ThrowErrorDefaultCloudFormationNode = new ThrowErrorDefaultCloudFormationNode(
303-
new DefaultRegionNode(new RegionInfo('code', 'name'))
304-
)
300+
const testNode: ThrowErrorDefaultCloudFormationNode = new ThrowErrorDefaultCloudFormationNode()
305301

306302
const childNodes = await testNode.getChildren()
307303
assert(childNodes !== undefined)

src/test/lambda/explorer/lambdaNodes.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import * as assert from 'assert'
77
import { Lambda } from 'aws-sdk'
88
import * as os from 'os'
9-
import { DefaultRegionNode } from '../../../awsexplorer/defaultRegionNode'
109
import {
1110
DefaultLambdaFunctionGroupNode,
1211
DefaultLambdaFunctionNode,
@@ -17,9 +16,9 @@ import { EcsClient } from '../../../shared/clients/ecsClient'
1716
import { LambdaClient } from '../../../shared/clients/lambdaClient'
1817
import { StsClient } from '../../../shared/clients/stsClient'
1918
import { ext } from '../../../shared/extensionGlobals'
20-
import { RegionInfo } from '../../../shared/regions/regionInfo'
2119
import { ErrorNode } from '../../../shared/treeview/nodes/errorNode'
2220
import { MockLambdaClient } from '../../shared/clients/mockClients'
21+
import { TestAWSTreeNode } from '../../shared/treeview/nodes/testAWSTreeNode'
2322
import { clearTestIconPaths, IconPath, setupTestIconPaths } from '../../shared/utilities/iconPathUtils'
2423

2524
// TODO : Consolidate all asyncGenerator calls into a shared utility method
@@ -80,10 +79,9 @@ describe('DefaultLambdaFunctionNode', () => {
8079
})
8180

8281
function generateTestNode(): DefaultLambdaFunctionNode {
83-
return new DefaultLambdaFunctionNode(
84-
new DefaultLambdaFunctionGroupNode(new DefaultRegionNode(new RegionInfo('code', 'name'))),
85-
fakeFunctionConfig
86-
)
82+
const parentNode = new TestAWSTreeNode('test node')
83+
84+
return new DefaultLambdaFunctionNode(parentNode, 'someregioncode', fakeFunctionConfig)
8785
}
8886
})
8987

@@ -107,8 +105,8 @@ describe('DefaultLambdaFunctionGroupNode', () => {
107105
}
108106

109107
class ThrowErrorDefaultLambdaFunctionGroupNode extends DefaultLambdaFunctionGroupNode {
110-
public constructor(public readonly parent: DefaultRegionNode) {
111-
super(parent)
108+
public constructor() {
109+
super('someregioncode')
112110
}
113111

114112
public async updateChildren(): Promise<void> {
@@ -138,9 +136,7 @@ describe('DefaultLambdaFunctionGroupNode', () => {
138136
}
139137
}
140138

141-
const functionGroupNode = new DefaultLambdaFunctionGroupNode(
142-
new DefaultRegionNode(new RegionInfo('code', 'name'))
143-
)
139+
const functionGroupNode = new DefaultLambdaFunctionGroupNode('someregioncode')
144140

145141
const children = await functionGroupNode.getChildren()
146142

@@ -176,9 +172,7 @@ describe('DefaultLambdaFunctionGroupNode', () => {
176172
})
177173

178174
it('handles error', async () => {
179-
const testNode = new ThrowErrorDefaultLambdaFunctionGroupNode(
180-
new DefaultRegionNode(new RegionInfo('code', 'name'))
181-
)
175+
const testNode = new ThrowErrorDefaultLambdaFunctionGroupNode()
182176

183177
const childNodes = await testNode.getChildren()
184178
assert(childNodes !== undefined)

src/test/lambda/explorer/mockLambdaNodes.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/test/shared/treeview/nodes/errorNode.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,25 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { RegionInfo } from '../../../../shared/regions/regionInfo'
87
import { ErrorNode } from '../../../../shared/treeview/nodes/errorNode'
9-
import { RegionNode } from '../../../../shared/treeview/nodes/regionNode'
8+
import { TestAWSTreeNode } from './testAWSTreeNode'
109

1110
describe('ErrorNode', () => {
12-
const regionNode: RegionNode = {
13-
regionCode: 'us-weast-1',
14-
regionName: 'East? I thought you said...weast!',
15-
update: (info: RegionInfo) => {},
16-
getChildren: async () => Promise.resolve([])
17-
}
18-
11+
const parentNode = new TestAWSTreeNode('test parent node')
1912
const error = new Error('error message')
2013
error.name = 'myMockError'
2114

2215
// Validates we tagged the node correctly
2316
it('initializes label and tooltip', async () => {
24-
const testNode = new ErrorNode(regionNode, error, 'Error loading resources')
17+
const testNode = new ErrorNode(parentNode, error, 'Error loading resources')
2518

2619
assert.strictEqual(testNode.label, 'Error loading resources')
2720
assert.strictEqual(testNode.tooltip, `${error.name}:${error.message}`)
2821
})
2922

3023
// Validates function nodes are leaves
3124
it('has no children', async () => {
32-
const testNode = new ErrorNode(regionNode, error, `Error loading resources (${error.name})`)
25+
const testNode = new ErrorNode(parentNode, error, `Error loading resources (${error.name})`)
3326

3427
const childNodes = await testNode.getChildren()
3528
assert(childNodes !== undefined)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*!
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { AWSTreeNodeBase } from '../../../../shared/treeview/nodes/awsTreeNodeBase'
7+
8+
export class TestAWSTreeNode extends AWSTreeNodeBase {
9+
public constructor(label: string) {
10+
super(label)
11+
}
12+
}

0 commit comments

Comments
 (0)