|
| 1 | +import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; |
| 2 | +import { Tree } from '../../generators/tree'; |
| 3 | +import addPolygraphToGitIgnore from './add-polygraph-to-git-ignore'; |
| 4 | + |
| 5 | +describe('add-polygraph-to-git-ignore migration', () => { |
| 6 | + let tree: Tree; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + tree = createTreeWithEmptyWorkspace(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should not create .gitignore if it does not exist', () => { |
| 13 | + tree.delete('.gitignore'); |
| 14 | + |
| 15 | + addPolygraphToGitIgnore(tree); |
| 16 | + |
| 17 | + expect(tree.exists('.gitignore')).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should add .nx/polygraph to existing .gitignore', () => { |
| 21 | + tree.write('.gitignore', 'node_modules\ndist\n'); |
| 22 | + |
| 23 | + addPolygraphToGitIgnore(tree); |
| 24 | + |
| 25 | + const gitignore = tree.read('.gitignore')?.toString(); |
| 26 | + expect(gitignore).toContain('node_modules'); |
| 27 | + expect(gitignore).toContain('.nx/polygraph'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should not duplicate if .nx/polygraph is already present', () => { |
| 31 | + tree.write('.gitignore', 'node_modules\n.nx/polygraph\n'); |
| 32 | + |
| 33 | + addPolygraphToGitIgnore(tree); |
| 34 | + |
| 35 | + const gitignore = tree.read('.gitignore')?.toString(); |
| 36 | + const matches = gitignore.match(/\.nx\/polygraph/g); |
| 37 | + expect(matches).toHaveLength(1); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not add if a broader pattern already covers it', () => { |
| 41 | + tree.write('.gitignore', '.nx/\n'); |
| 42 | + |
| 43 | + addPolygraphToGitIgnore(tree); |
| 44 | + |
| 45 | + const gitignore = tree.read('.gitignore')?.toString(); |
| 46 | + expect(gitignore).not.toContain('.nx/polygraph'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should skip for lerna workspaces without nx.json', () => { |
| 50 | + tree.write('.gitignore', 'node_modules\n'); |
| 51 | + tree.write('lerna.json', '{}'); |
| 52 | + tree.delete('nx.json'); |
| 53 | + |
| 54 | + addPolygraphToGitIgnore(tree); |
| 55 | + |
| 56 | + const gitignore = tree.read('.gitignore')?.toString(); |
| 57 | + expect(gitignore).not.toContain('.nx/polygraph'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should not skip for lerna workspaces that also have nx.json', () => { |
| 61 | + tree.write('.gitignore', 'node_modules\n'); |
| 62 | + tree.write('lerna.json', '{}'); |
| 63 | + // nx.json already exists from createTreeWithEmptyWorkspace |
| 64 | + |
| 65 | + addPolygraphToGitIgnore(tree); |
| 66 | + |
| 67 | + const gitignore = tree.read('.gitignore')?.toString(); |
| 68 | + expect(gitignore).toContain('.nx/polygraph'); |
| 69 | + }); |
| 70 | +}); |
0 commit comments