|
| 1 | +import { ActionConfigZ } from '@expo/eas-build-job'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { |
| 5 | + buildActionCatalogFromStepsAsync, |
| 6 | + isActionReference, |
| 7 | + isLocalActionPathWithinProject, |
| 8 | + parseActionReference, |
| 9 | + resolveLocalActionPath, |
| 10 | +} from '../localActions'; |
| 11 | + |
| 12 | +describe(parseActionReference, () => { |
| 13 | + it('parses local action references', () => { |
| 14 | + expect(parseActionReference('./.eas/actions/setup')).toBe('./.eas/actions/setup'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('normalizes local action references', () => { |
| 18 | + expect(parseActionReference(' ./.eas/actions/setup/ ')).toBe('./.eas/actions/setup'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns null for built-in function ids', () => { |
| 22 | + expect(parseActionReference('eas/build')).toBeNull(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns null for degenerate refs with no path segment', () => { |
| 26 | + expect(parseActionReference('./')).toBeNull(); |
| 27 | + expect(parseActionReference(' ./ ')).toBeNull(); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe(isActionReference, () => { |
| 32 | + it('returns true for local action references', () => { |
| 33 | + expect(isActionReference('./.eas/actions/setup')).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns false for built-in function ids', () => { |
| 37 | + expect(isActionReference('eas/build')).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns false for degenerate refs with no path segment', () => { |
| 41 | + expect(isActionReference('./')).toBe(false); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe(buildActionCatalogFromStepsAsync, () => { |
| 46 | + it('loads referenced actions transitively', async () => { |
| 47 | + const catalog = await buildActionCatalogFromStepsAsync({ |
| 48 | + rootSteps: [{ uses: './.eas/actions/outer', id: 'outer' }], |
| 49 | + loadAction: async ref => { |
| 50 | + if (ref === './.eas/actions/outer') { |
| 51 | + return ActionConfigZ.parse({ |
| 52 | + runs: { steps: [{ uses: './.eas/actions/inner' }] }, |
| 53 | + }); |
| 54 | + } |
| 55 | + if (ref === './.eas/actions/inner') { |
| 56 | + return ActionConfigZ.parse({ |
| 57 | + runs: { steps: [{ run: 'echo inner' }] }, |
| 58 | + }); |
| 59 | + } |
| 60 | + throw new Error(`missing ${ref}`); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(Object.keys(catalog).sort()).toEqual(['./.eas/actions/inner', './.eas/actions/outer']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('detects cycles while loading', async () => { |
| 68 | + await expect( |
| 69 | + buildActionCatalogFromStepsAsync({ |
| 70 | + rootSteps: [{ uses: './.eas/actions/a', id: 'a' }], |
| 71 | + loadAction: async ref => { |
| 72 | + if (ref === './.eas/actions/a') { |
| 73 | + return ActionConfigZ.parse({ |
| 74 | + runs: { steps: [{ uses: './.eas/actions/b' }] }, |
| 75 | + }); |
| 76 | + } |
| 77 | + if (ref === './.eas/actions/b') { |
| 78 | + return ActionConfigZ.parse({ |
| 79 | + runs: { steps: [{ uses: './.eas/actions/a' }] }, |
| 80 | + }); |
| 81 | + } |
| 82 | + throw new Error(`missing ${ref}`); |
| 83 | + }, |
| 84 | + }) |
| 85 | + ).rejects.toThrow(/cycle/i); |
| 86 | + }); |
| 87 | + |
| 88 | + it('rejects action chains deeper than the maximum nesting depth at catalog build time', async () => { |
| 89 | + const chainLength = 12; |
| 90 | + const refs = Array.from({ length: chainLength }, (_, index) => `./.eas/actions/a${index}`); |
| 91 | + |
| 92 | + await expect( |
| 93 | + buildActionCatalogFromStepsAsync({ |
| 94 | + rootSteps: [{ uses: refs[0], id: 'root' }], |
| 95 | + loadAction: async ref => { |
| 96 | + const index = refs.indexOf(ref); |
| 97 | + if (index === -1) { |
| 98 | + throw new Error(`missing ${ref}`); |
| 99 | + } |
| 100 | + if (index === chainLength - 1) { |
| 101 | + return ActionConfigZ.parse({ runs: { steps: [{ run: 'echo leaf' }] } }); |
| 102 | + } |
| 103 | + return ActionConfigZ.parse({ |
| 104 | + runs: { steps: [{ uses: refs[index + 1] }] }, |
| 105 | + }); |
| 106 | + }, |
| 107 | + }) |
| 108 | + ).rejects.toThrow(/Maximum action nesting depth \(10\) exceeded/); |
| 109 | + }); |
| 110 | + |
| 111 | + it('collects normalized action references from steps', async () => { |
| 112 | + const loadedRefs: string[] = []; |
| 113 | + await buildActionCatalogFromStepsAsync({ |
| 114 | + rootSteps: [{ uses: './.eas/actions/setup/' }, { uses: 'eas/build' }, { run: 'echo hi' }], |
| 115 | + loadAction: async ref => { |
| 116 | + loadedRefs.push(ref); |
| 117 | + return ActionConfigZ.parse({ runs: { steps: [{ run: 'echo setup' }] } }); |
| 118 | + }, |
| 119 | + }); |
| 120 | + expect(loadedRefs).toEqual(['./.eas/actions/setup']); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe(resolveLocalActionPath, () => { |
| 125 | + const projectRoot = path.resolve('/tmp/project'); |
| 126 | + |
| 127 | + it('resolves a reference under the conventional .eas/actions directory', () => { |
| 128 | + expect(resolveLocalActionPath(projectRoot, './.eas/actions/setup')).toBe( |
| 129 | + path.join(projectRoot, '.eas', 'actions', 'setup') |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('resolves an arbitrary GitHub Actions style path within the project', () => { |
| 134 | + expect(resolveLocalActionPath(projectRoot, './internal-actions/deploy')).toBe( |
| 135 | + path.join(projectRoot, 'internal-actions', 'deploy') |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe(isLocalActionPathWithinProject, () => { |
| 141 | + const projectRoot = path.resolve('/tmp/project'); |
| 142 | + |
| 143 | + it('returns true for paths within the project', () => { |
| 144 | + expect( |
| 145 | + isLocalActionPathWithinProject( |
| 146 | + projectRoot, |
| 147 | + resolveLocalActionPath(projectRoot, './.eas/actions/setup') |
| 148 | + ) |
| 149 | + ).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('returns false when the reference escapes the project root via ..', () => { |
| 153 | + expect( |
| 154 | + isLocalActionPathWithinProject( |
| 155 | + projectRoot, |
| 156 | + resolveLocalActionPath(projectRoot, './../secrets') |
| 157 | + ) |
| 158 | + ).toBe(false); |
| 159 | + }); |
| 160 | + |
| 161 | + it('returns false when the reference resolves to the project root itself', () => { |
| 162 | + expect( |
| 163 | + isLocalActionPathWithinProject(projectRoot, resolveLocalActionPath(projectRoot, './')) |
| 164 | + ).toBe(false); |
| 165 | + }); |
| 166 | +}); |
0 commit comments