Skip to content

Commit 283d265

Browse files
committed
add tests
1 parent 16e988d commit 283d265

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

test/Solution.spec.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
22
import { ProjectBuilder } from '../src/Structure/Project';
33
import { Solution } from '../src/Structure/Solution';
4+
import * as pathUtils from 'path';
45

56
describe('Solution Tests', () => {
67
const packageReferences: string = path.join(__dirname, 'resources', 'packagereferences');
@@ -78,4 +79,90 @@ describe('Solution Tests', () => {
7879
}
7980
}
8081
});
82+
83+
describe('loadProject dependencies source matching', () => {
84+
function createDummyProjectBuilder(
85+
name: string,
86+
rootPath: string,
87+
dependenciesSource: string
88+
): any {
89+
return {
90+
name,
91+
rootPath,
92+
dependenciesSource,
93+
extractor: true
94+
};
95+
}
96+
97+
test('matches dependencies source in project root', () => {
98+
const projectName: string = 'myproject';
99+
const rootPath: string = '/repo/projects/myproject';
100+
const csprojPath: string = pathUtils.join(rootPath, 'myproject.csproj');
101+
const depSource: string = pathUtils.join(rootPath, 'project.assets.json');
102+
const solution: Solution = new Solution('/repo/projects/solution.sln');
103+
solution.dependenciesSources = [depSource];
104+
// Patch ProjectBuilder.load to return dummy
105+
(ProjectBuilder as any).load = jest.fn(() => createDummyProjectBuilder(projectName, rootPath, depSource));
106+
solution.loadProject(projectName, csprojPath);
107+
expect(solution.projects).toHaveLength(1);
108+
expect(solution.projects[0].dependenciesSource).toBe(depSource);
109+
});
110+
111+
test('matches dependencies source under project directory', () => {
112+
const projectName: string = 'myproject';
113+
const rootPath: string = '/repo/projects/myproject';
114+
const csprojPath: string = pathUtils.join(rootPath, 'myproject.csproj');
115+
const depSource: string = pathUtils.join(rootPath, 'subdir', 'project.assets.json');
116+
const solution: Solution = new Solution('/repo/projects/solution.sln');
117+
solution.dependenciesSources = [depSource];
118+
(ProjectBuilder as any).load = jest.fn(() => createDummyProjectBuilder(projectName, rootPath, depSource));
119+
solution.loadProject(projectName, csprojPath);
120+
expect(solution.projects).toHaveLength(1);
121+
expect(solution.projects[0].dependenciesSource).toBe(depSource);
122+
});
123+
124+
test('matches dependencies source ending with project name', () => {
125+
const projectName: string = 'myproject';
126+
const rootPath: string = '/repo/projects/myproject';
127+
const csprojPath: string = pathUtils.join(rootPath, 'myproject.csproj');
128+
const depSource: string = '/repo/projects/otherdir/myproject';
129+
const solution: Solution = new Solution('/repo/projects/solution.sln');
130+
solution.dependenciesSources = [depSource];
131+
(ProjectBuilder as any).load = jest.fn(() => createDummyProjectBuilder(projectName, rootPath, depSource));
132+
solution.loadProject(projectName, csprojPath);
133+
expect(solution.projects).toHaveLength(1);
134+
expect(solution.projects[0].dependenciesSource).toBe(depSource);
135+
});
136+
137+
test('multiple sources, only correct one matched', () => {
138+
const projectName: string = 'myproject';
139+
const rootPath: string = '/repo/projects/myproject';
140+
const csprojPath: string = pathUtils.join(rootPath, 'myproject.csproj');
141+
const depSource1: string = '/repo/projects/otherdir/otherproject';
142+
const depSource2: string = pathUtils.join(rootPath, 'project.assets.json');
143+
const depSource3: string = '/repo/projects/otherdir/myproject';
144+
const solution: Solution = new Solution('/repo/projects/solution.sln');
145+
solution.dependenciesSources = [depSource1, depSource2, depSource3];
146+
(ProjectBuilder as any).load = jest.fn(() => createDummyProjectBuilder(projectName, rootPath, depSource2));
147+
solution.loadProject(projectName, csprojPath);
148+
expect(solution.projects).toHaveLength(1);
149+
expect(solution.projects[0].dependenciesSource).toBe(depSource2);
150+
});
151+
152+
test('no matching source, dependenciesSource is empty', () => {
153+
const projectName: string = 'myproject';
154+
const rootPath: string = '/repo/projects/myproject';
155+
const csprojPath: string = pathUtils.join(rootPath, 'myproject.csproj');
156+
const depSource: string = '/repo/projects/otherdir/otherproject';
157+
const solution: Solution = new Solution('/repo/projects/solution.sln');
158+
solution.dependenciesSources = [depSource];
159+
(ProjectBuilder as any).load = jest.fn((name: string, root: string, source: string) => {
160+
expect(source).toBe('');
161+
return createDummyProjectBuilder(name, root, source);
162+
});
163+
solution.loadProject(projectName, csprojPath);
164+
expect(solution.projects).toHaveLength(1);
165+
expect(solution.projects[0].dependenciesSource).toBe('');
166+
});
167+
});
81168
});

0 commit comments

Comments
 (0)