|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { skipCommentedUsages } from '../../../../src/core/scan/skipCommentedUsages.js'; |
| 3 | +import type { EnvUsage } from '../../../../src/config/types.js'; |
| 4 | + |
| 5 | +const usage = (variable: string, context?: string): EnvUsage => ({ |
| 6 | + variable, |
| 7 | + context, |
| 8 | +}); |
| 9 | + |
| 10 | +describe('skipCommentedUsages', () => { |
| 11 | + it('keeps usages without context', () => { |
| 12 | + const usages = [usage('API_URL'), usage('DB_HOST')]; |
| 13 | + expect(skipCommentedUsages(usages)).toEqual(usages); |
| 14 | + }); |
| 15 | + |
| 16 | + it('keeps normal (non-commented) usages', () => { |
| 17 | + const usages = [usage('API_URL', 'const url = process.env.API_URL')]; |
| 18 | + expect(skipCommentedUsages(usages)).toEqual(usages); |
| 19 | + }); |
| 20 | + |
| 21 | + // ---- single-line comment prefixes ---- |
| 22 | + |
| 23 | + it('filters // commented lines', () => { |
| 24 | + expect( |
| 25 | + skipCommentedUsages([usage('API_URL', '// process.env.API_URL')]), |
| 26 | + ).toEqual([]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('filters # commented lines', () => { |
| 30 | + expect( |
| 31 | + skipCommentedUsages([usage('API_URL', '# process.env.API_URL')]), |
| 32 | + ).toEqual([]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('filters /* commented lines', () => { |
| 36 | + expect( |
| 37 | + skipCommentedUsages([usage('API_URL', '/* process.env.API_URL')]), |
| 38 | + ).toEqual([]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('filters * commented lines (inside block comment)', () => { |
| 42 | + expect( |
| 43 | + skipCommentedUsages([usage('API_URL', ' * process.env.API_URL')]), |
| 44 | + ).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('filters <!-- commented lines', () => { |
| 48 | + expect( |
| 49 | + skipCommentedUsages([usage('API_URL', '<!-- process.env.API_URL -->')]), |
| 50 | + ).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('filters leading whitespace before comment prefix', () => { |
| 54 | + expect( |
| 55 | + skipCommentedUsages([usage('API_URL', ' // process.env.API_URL')]), |
| 56 | + ).toEqual([]); |
| 57 | + expect( |
| 58 | + skipCommentedUsages([usage('API_URL', ' # process.env.API_URL')]), |
| 59 | + ).toEqual([]); |
| 60 | + expect( |
| 61 | + skipCommentedUsages([usage('API_URL', ' /* process.env.API_URL')]), |
| 62 | + ).toEqual([]); |
| 63 | + }); |
| 64 | + |
| 65 | + // ---- dotenv-diff-ignore inline comment ---- |
| 66 | + |
| 67 | + it('filters lines with // dotenv-diff-ignore', () => { |
| 68 | + expect( |
| 69 | + skipCommentedUsages([ |
| 70 | + usage('API_URL', 'process.env.API_URL // dotenv-diff-ignore'), |
| 71 | + ]), |
| 72 | + ).toEqual([]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('filters lines with <!-- dotenv-diff-ignore --> inline', () => { |
| 76 | + expect( |
| 77 | + skipCommentedUsages([ |
| 78 | + usage('API_URL', 'process.env.API_URL <!-- dotenv-diff-ignore -->'), |
| 79 | + ]), |
| 80 | + ).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + // ---- HTML comment blocks ---- |
| 84 | + |
| 85 | + it('filters lines inside an open HTML comment', () => { |
| 86 | + const usages = [ |
| 87 | + usage('BEFORE', 'const a = process.env.BEFORE'), |
| 88 | + usage('START', '<!-- start of comment'), |
| 89 | + usage('INSIDE', 'process.env.API_URL'), |
| 90 | + usage('END', '-->'), |
| 91 | + usage('AFTER', 'const b = process.env.AFTER'), |
| 92 | + ]; |
| 93 | + const result = skipCommentedUsages(usages); |
| 94 | + expect(result.map((u) => u.variable)).toEqual(['BEFORE', 'AFTER']); |
| 95 | + }); |
| 96 | + |
| 97 | + it('filters the --> closing line itself', () => { |
| 98 | + const usages = [ |
| 99 | + usage('A', '<!--'), |
| 100 | + usage('B', 'process.env.SECRET'), |
| 101 | + usage('C', '-->'), |
| 102 | + ]; |
| 103 | + const result = skipCommentedUsages(usages); |
| 104 | + expect(result).toEqual([]); |
| 105 | + }); |
| 106 | + |
| 107 | + // ---- dotenv-diff-ignore-start / end blocks ---- |
| 108 | + |
| 109 | + it('filters usages inside ignore-start/end block', () => { |
| 110 | + const usages = [ |
| 111 | + usage('BEFORE', 'process.env.BEFORE'), |
| 112 | + usage('START', '<!-- dotenv-diff-ignore-start -->'), |
| 113 | + usage('INSIDE', 'process.env.INSIDE'), |
| 114 | + usage('END', '<!-- dotenv-diff-ignore-end -->'), |
| 115 | + usage('AFTER', 'process.env.AFTER'), |
| 116 | + ]; |
| 117 | + const result = skipCommentedUsages(usages); |
| 118 | + expect(result.map((u) => u.variable)).toEqual(['BEFORE', 'AFTER']); |
| 119 | + }); |
| 120 | + |
| 121 | + it('also removes the start and end marker lines themselves', () => { |
| 122 | + const usages = [ |
| 123 | + usage('START', '<!-- dotenv-diff-ignore-start -->'), |
| 124 | + usage('END', '<!-- dotenv-diff-ignore-end -->'), |
| 125 | + ]; |
| 126 | + expect(skipCommentedUsages(usages)).toEqual([]); |
| 127 | + }); |
| 128 | + |
| 129 | + it('is case-insensitive for ignore block markers', () => { |
| 130 | + const usages = [ |
| 131 | + usage('START', '<!-- DOTENV-DIFF-IGNORE-START -->'), |
| 132 | + usage('INSIDE', 'process.env.INSIDE'), |
| 133 | + usage('END', '<!-- DOTENV-DIFF-IGNORE-END -->'), |
| 134 | + ]; |
| 135 | + expect(skipCommentedUsages(usages)).toEqual([]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('accepts dashes and spaces in ignore block markers', () => { |
| 139 | + const usages = [ |
| 140 | + usage('START', '<!-- dotenv diff ignore start -->'), |
| 141 | + usage('INSIDE', 'process.env.INSIDE'), |
| 142 | + usage('END', '<!-- dotenv diff ignore end -->'), |
| 143 | + ]; |
| 144 | + expect(skipCommentedUsages(usages)).toEqual([]); |
| 145 | + }); |
| 146 | + |
| 147 | + // ---- mixed scenarios ---- |
| 148 | + |
| 149 | + it('keeps usages that come after the ignore block ends', () => { |
| 150 | + const usages = [ |
| 151 | + usage('IN', '<!-- dotenv-diff-ignore-start -->'), |
| 152 | + usage('IGNORED', 'process.env.IGNORED'), |
| 153 | + usage('OUT', '<!-- dotenv-diff-ignore-end -->'), |
| 154 | + usage('KEPT', 'process.env.KEPT'), |
| 155 | + ]; |
| 156 | + const result = skipCommentedUsages(usages); |
| 157 | + expect(result.map((u) => u.variable)).toEqual(['KEPT']); |
| 158 | + }); |
| 159 | + |
| 160 | + it('returns empty array when all usages are commented', () => { |
| 161 | + const usages = [ |
| 162 | + usage('A', '// process.env.A'), |
| 163 | + usage('B', '# process.env.B'), |
| 164 | + usage('C', '/* process.env.C'), |
| 165 | + ]; |
| 166 | + expect(skipCommentedUsages(usages)).toEqual([]); |
| 167 | + }); |
| 168 | + |
| 169 | + it('returns all usages when none are commented', () => { |
| 170 | + const usages = [ |
| 171 | + usage('A', 'doSomething(process.env.A)'), |
| 172 | + usage('B', 'const x = process.env.B'), |
| 173 | + ]; |
| 174 | + expect(skipCommentedUsages(usages)).toEqual(usages); |
| 175 | + }); |
| 176 | + |
| 177 | + it('handles empty array', () => { |
| 178 | + expect(skipCommentedUsages([])).toEqual([]); |
| 179 | + }); |
| 180 | +}); |
0 commit comments