|
1 | 1 | import { Command } from 'commander'; |
2 | | -import { parseEnvironmentVariables, parseDomains, escapeShellArg, joinShellArgs, parseVolumeMounts } from './cli'; |
| 2 | +import { parseEnvironmentVariables, parseDomains, parseDomainsFile, escapeShellArg, joinShellArgs, parseVolumeMounts } from './cli'; |
3 | 3 | import { redactSecrets } from './redact-secrets'; |
4 | 4 | import * as fs from 'fs'; |
5 | 5 | import * as path from 'path'; |
@@ -38,6 +38,127 @@ describe('cli', () => { |
38 | 38 | }); |
39 | 39 | }); |
40 | 40 |
|
| 41 | + describe('domain file parsing', () => { |
| 42 | + let testDir: string; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + // Create a temporary directory for testing |
| 46 | + testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'awf-test-')); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + // Clean up the test directory |
| 51 | + if (fs.existsSync(testDir)) { |
| 52 | + fs.rmSync(testDir, { recursive: true, force: true }); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + it('should parse domains from file with one domain per line', () => { |
| 57 | + const filePath = path.join(testDir, 'domains.txt'); |
| 58 | + fs.writeFileSync(filePath, 'github.com\napi.github.com\nnpmjs.org'); |
| 59 | + |
| 60 | + const result = parseDomainsFile(filePath); |
| 61 | + |
| 62 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should parse comma-separated domains from file', () => { |
| 66 | + const filePath = path.join(testDir, 'domains.txt'); |
| 67 | + fs.writeFileSync(filePath, 'github.com, api.github.com, npmjs.org'); |
| 68 | + |
| 69 | + const result = parseDomainsFile(filePath); |
| 70 | + |
| 71 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle mixed formats (lines and commas)', () => { |
| 75 | + const filePath = path.join(testDir, 'domains.txt'); |
| 76 | + fs.writeFileSync(filePath, 'github.com\napi.github.com, npmjs.org\nexample.com'); |
| 77 | + |
| 78 | + const result = parseDomainsFile(filePath); |
| 79 | + |
| 80 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org', 'example.com']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should skip empty lines', () => { |
| 84 | + const filePath = path.join(testDir, 'domains.txt'); |
| 85 | + fs.writeFileSync(filePath, 'github.com\n\n\napi.github.com\n\nnpmjs.org'); |
| 86 | + |
| 87 | + const result = parseDomainsFile(filePath); |
| 88 | + |
| 89 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should skip lines with only whitespace', () => { |
| 93 | + const filePath = path.join(testDir, 'domains.txt'); |
| 94 | + fs.writeFileSync(filePath, 'github.com\n \n\t\napi.github.com'); |
| 95 | + |
| 96 | + const result = parseDomainsFile(filePath); |
| 97 | + |
| 98 | + expect(result).toEqual(['github.com', 'api.github.com']); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should skip comments starting with #', () => { |
| 102 | + const filePath = path.join(testDir, 'domains.txt'); |
| 103 | + fs.writeFileSync(filePath, '# This is a comment\ngithub.com\n# Another comment\napi.github.com'); |
| 104 | + |
| 105 | + const result = parseDomainsFile(filePath); |
| 106 | + |
| 107 | + expect(result).toEqual(['github.com', 'api.github.com']); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should handle inline comments (after domain)', () => { |
| 111 | + const filePath = path.join(testDir, 'domains.txt'); |
| 112 | + fs.writeFileSync(filePath, 'github.com # GitHub main domain\napi.github.com # API endpoint'); |
| 113 | + |
| 114 | + const result = parseDomainsFile(filePath); |
| 115 | + |
| 116 | + expect(result).toEqual(['github.com', 'api.github.com']); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle domains with inline comments in comma-separated format', () => { |
| 120 | + const filePath = path.join(testDir, 'domains.txt'); |
| 121 | + fs.writeFileSync(filePath, 'github.com, api.github.com # GitHub domains\nnpmjs.org'); |
| 122 | + |
| 123 | + const result = parseDomainsFile(filePath); |
| 124 | + |
| 125 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should throw error if file does not exist', () => { |
| 129 | + const nonExistentPath = path.join(testDir, 'nonexistent.txt'); |
| 130 | + |
| 131 | + expect(() => parseDomainsFile(nonExistentPath)).toThrow('Domains file not found'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return empty array for file with only comments and whitespace', () => { |
| 135 | + const filePath = path.join(testDir, 'domains.txt'); |
| 136 | + fs.writeFileSync(filePath, '# Comment 1\n\n# Comment 2\n \n'); |
| 137 | + |
| 138 | + const result = parseDomainsFile(filePath); |
| 139 | + |
| 140 | + expect(result).toEqual([]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle file with Windows line endings (CRLF)', () => { |
| 144 | + const filePath = path.join(testDir, 'domains.txt'); |
| 145 | + fs.writeFileSync(filePath, 'github.com\r\napi.github.com\r\nnpmjs.org'); |
| 146 | + |
| 147 | + const result = parseDomainsFile(filePath); |
| 148 | + |
| 149 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should trim whitespace from each domain', () => { |
| 153 | + const filePath = path.join(testDir, 'domains.txt'); |
| 154 | + fs.writeFileSync(filePath, ' github.com \n api.github.com \n npmjs.org '); |
| 155 | + |
| 156 | + const result = parseDomainsFile(filePath); |
| 157 | + |
| 158 | + expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
41 | 162 | describe('environment variable parsing', () => { |
42 | 163 | it('should parse KEY=VALUE format correctly', () => { |
43 | 164 | const envVars = ['GITHUB_TOKEN=abc123', 'API_KEY=xyz789']; |
|
0 commit comments