|
1 | 1 | import { describe, expect, it } from 'vitest';
|
2 |
| -import { metaDescription, tableSection } from './formatting'; |
| 2 | +import { toUnixPath } from '../transform'; |
| 3 | +import { |
| 4 | + formatFileLink, |
| 5 | + formatGitHubLink, |
| 6 | + formatSourceLine, |
| 7 | + linkToLocalSourceForIde, |
| 8 | + metaDescription, |
| 9 | + tableSection, |
| 10 | +} from './formatting'; |
3 | 11 |
|
4 | 12 | describe('tableSection', () => {
|
5 | 13 | it('should accept a title', () => {
|
@@ -112,3 +120,171 @@ describe('metaDescription', () => {
|
112 | 120 | );
|
113 | 121 | });
|
114 | 122 | });
|
| 123 | + |
| 124 | +describe('formatSourceLine', () => { |
| 125 | + it.each([ |
| 126 | + [{ startLine: 2 }, '2'], |
| 127 | + [{ startLine: 2, endLine: undefined }, '2'], |
| 128 | + [{ startLine: 2, endLine: 2 }, '2'], |
| 129 | + [{ startLine: 2, endLine: 3 }, '2-3'], |
| 130 | + ])('should format position %o as "%s"', (position, expected) => { |
| 131 | + expect(formatSourceLine(position)).toBe(expected); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return an empty string when the position is missing', () => { |
| 135 | + expect(formatSourceLine(undefined)).toBe(''); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('linkToLocalSourceForIde', () => { |
| 140 | + it('should not format the file path as a link when outputDir is undefined (VS Code)', () => { |
| 141 | + vi.stubEnv('TERM_PROGRAM', 'vscode'); |
| 142 | + vi.stubEnv('GITHUB_ACTIONS', 'false'); |
| 143 | + |
| 144 | + expect( |
| 145 | + linkToLocalSourceForIde({ |
| 146 | + file: toUnixPath('packages/utils/src/index.ts'), |
| 147 | + }).toString(), |
| 148 | + ).toBe('`packages/utils/src/index.ts`'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should format the file path as a link when outputDir is defined (VS Code)', () => { |
| 152 | + vi.stubEnv('TERM_PROGRAM', 'vscode'); |
| 153 | + vi.stubEnv('GITHUB_ACTIONS', 'false'); |
| 154 | + const filePath = toUnixPath('packages/utils/src/index.ts'); |
| 155 | + const outputDir = toUnixPath('.code-pushup'); |
| 156 | + |
| 157 | + expect( |
| 158 | + linkToLocalSourceForIde({ file: filePath }, { outputDir }).toString(), |
| 159 | + ).toBe('[`packages/utils/src/index.ts`](../packages/utils/src/index.ts)'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should return a link to a specific line when startLine is provided (VS Code)', () => { |
| 163 | + vi.stubEnv('TERM_PROGRAM', 'vscode'); |
| 164 | + vi.stubEnv('GITHUB_ACTIONS', 'false'); |
| 165 | + const filePath = toUnixPath('packages/utils/src/index.ts'); |
| 166 | + const outputDir = toUnixPath('.code-pushup'); |
| 167 | + |
| 168 | + expect( |
| 169 | + linkToLocalSourceForIde( |
| 170 | + { file: filePath, position: { startLine: 2 } }, |
| 171 | + { outputDir }, |
| 172 | + ).toString(), |
| 173 | + ).toBe( |
| 174 | + '[`packages/utils/src/index.ts`](../packages/utils/src/index.ts#L2)', |
| 175 | + ); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +describe('formatGitHubLink', () => { |
| 180 | + beforeEach(() => { |
| 181 | + vi.stubEnv('TERM_PROGRAM', ''); |
| 182 | + vi.stubEnv('GITHUB_ACTIONS', 'true'); |
| 183 | + vi.stubEnv('GITHUB_SERVER_URL', 'https://github.com'); |
| 184 | + vi.stubEnv('GITHUB_REPOSITORY', 'user/repo'); |
| 185 | + vi.stubEnv('GITHUB_SHA', '1234567890abcdef'); |
| 186 | + }); |
| 187 | + |
| 188 | + it.each([ |
| 189 | + [ |
| 190 | + { startLine: 2 }, |
| 191 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2', |
| 192 | + ], |
| 193 | + [ |
| 194 | + { startLine: 2, endLine: 5 }, |
| 195 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2-L5', |
| 196 | + ], |
| 197 | + [ |
| 198 | + { startLine: 2, startColumn: 1 }, |
| 199 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2C1', |
| 200 | + ], |
| 201 | + [ |
| 202 | + { startLine: 2, endLine: 2, startColumn: 1, endColumn: 5 }, |
| 203 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2C1-L2C5', |
| 204 | + ], |
| 205 | + [ |
| 206 | + { startLine: 2, endLine: 5, startColumn: 1, endColumn: 6 }, |
| 207 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2C1-L5C6', |
| 208 | + ], |
| 209 | + [ |
| 210 | + { startLine: 2, endLine: 2, startColumn: 1, endColumn: 1 }, |
| 211 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2C1', |
| 212 | + ], |
| 213 | + ])( |
| 214 | + 'should generate a GitHub repository link for the file with position %o', |
| 215 | + (position, expected) => { |
| 216 | + expect(formatGitHubLink(toUnixPath('src/index.ts'), position)).toBe( |
| 217 | + expected, |
| 218 | + ); |
| 219 | + }, |
| 220 | + ); |
| 221 | + |
| 222 | + it('should generate a GitHub repository link for the file when the position is undefined', () => { |
| 223 | + expect(formatGitHubLink(toUnixPath('src/index.ts'), undefined)).toBe( |
| 224 | + 'https://github.com/user/repo/blob/1234567890abcdef/src/index.ts', |
| 225 | + ); |
| 226 | + }); |
| 227 | +}); |
| 228 | + |
| 229 | +describe('formatFileLink', () => { |
| 230 | + it('should return a GitHub repository link when running in GitHub Actions', () => { |
| 231 | + vi.stubEnv('TERM_PROGRAM', ''); |
| 232 | + vi.stubEnv('GITHUB_ACTIONS', 'true'); |
| 233 | + vi.stubEnv('GITHUB_SERVER_URL', 'https://github.com'); |
| 234 | + vi.stubEnv('GITHUB_REPOSITORY', 'user/repo'); |
| 235 | + vi.stubEnv('GITHUB_SHA', '1234567890abcdef'); |
| 236 | + |
| 237 | + expect( |
| 238 | + formatFileLink( |
| 239 | + toUnixPath('src/index.ts'), |
| 240 | + { startLine: 2 }, |
| 241 | + toUnixPath('.code-pushup'), |
| 242 | + ), |
| 243 | + ).toBe( |
| 244 | + `https://github.com/user/repo/blob/1234567890abcdef/src/index.ts#L2`, |
| 245 | + ); |
| 246 | + }); |
| 247 | + |
| 248 | + it.each([ |
| 249 | + [{ startLine: 2 }, '../src/index.ts#L2'], |
| 250 | + [{ startLine: 2, endLine: 5 }, '../src/index.ts#L2'], |
| 251 | + [{ startLine: 2, startColumn: 1 }, '../src/index.ts#L2'], |
| 252 | + ])( |
| 253 | + 'should transform the file path by including position %o when running in VS Code', |
| 254 | + (position, expected) => { |
| 255 | + vi.stubEnv('TERM_PROGRAM', 'vscode'); |
| 256 | + vi.stubEnv('GITHUB_ACTIONS', 'false'); |
| 257 | + expect( |
| 258 | + formatFileLink( |
| 259 | + toUnixPath('src/index.ts'), |
| 260 | + position, |
| 261 | + toUnixPath('.code-pushup'), |
| 262 | + ), |
| 263 | + ).toBe(expected); |
| 264 | + }, |
| 265 | + ); |
| 266 | + |
| 267 | + it('should return a relative file path when the position is undefined (VS Code)', () => { |
| 268 | + vi.stubEnv('TERM_PROGRAM', 'vscode'); |
| 269 | + vi.stubEnv('GITHUB_ACTIONS', 'false'); |
| 270 | + expect( |
| 271 | + formatFileLink( |
| 272 | + toUnixPath('src/index.ts'), |
| 273 | + undefined, |
| 274 | + toUnixPath('.code-pushup'), |
| 275 | + ), |
| 276 | + ).toBe('../src/index.ts'); |
| 277 | + }); |
| 278 | + |
| 279 | + it('should return a relative file path when the environment is neither VS Code nor GitHub', () => { |
| 280 | + vi.stubEnv('TERM_PROGRAM', ''); |
| 281 | + vi.stubEnv('GITHUB_ACTIONS', 'false'); |
| 282 | + expect( |
| 283 | + formatFileLink( |
| 284 | + toUnixPath('src/index.ts'), |
| 285 | + { startLine: 2, startColumn: 1 }, |
| 286 | + toUnixPath('.code-pushup'), |
| 287 | + ), |
| 288 | + ).toBe('../src/index.ts'); |
| 289 | + }); |
| 290 | +}); |
0 commit comments