-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docker setup in Setup Script (#3187)
* first commit * migrated jest to vitest in validateRecaptcha
- Loading branch information
1 parent
c8a1290
commit 2f68501
Showing
10 changed files
with
507 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/setup/askAndSetDockerOption/askAndSetDockerOption.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
|
||
// Mock modules | ||
vi.mock('inquirer', () => ({ | ||
default: { | ||
prompt: vi.fn(), | ||
}, | ||
})); | ||
|
||
vi.mock('setup/updateEnvFile/updateEnvFile', () => ({ | ||
default: vi.fn(), | ||
})); | ||
|
||
vi.mock('setup/askForDocker/askForDocker', () => ({ | ||
askForDocker: vi.fn(), | ||
})); | ||
|
||
// Import after mocking | ||
import askAndSetDockerOption from './askAndSetDockerOption'; | ||
import inquirer from 'inquirer'; | ||
import updateEnvFile from 'setup/updateEnvFile/updateEnvFile'; | ||
import { askForDocker } from 'setup/askForDocker/askForDocker'; | ||
|
||
describe('askAndSetDockerOption', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should set up Docker when user selects yes', async () => { | ||
(inquirer.prompt as unknown as jest.Mock).mockResolvedValueOnce({ | ||
useDocker: true, | ||
}); | ||
(askForDocker as jest.Mock).mockResolvedValueOnce(8080); | ||
|
||
await askAndSetDockerOption(); | ||
|
||
expect(updateEnvFile).toHaveBeenCalledWith('USE_DOCKER', 'YES'); | ||
expect(updateEnvFile).toHaveBeenCalledWith('DOCKER_PORT', 8080); | ||
}); | ||
|
||
it('should set up without Docker when user selects no', async () => { | ||
(inquirer.prompt as unknown as jest.Mock).mockResolvedValueOnce({ | ||
useDocker: false, | ||
}); | ||
|
||
await askAndSetDockerOption(); | ||
|
||
expect(updateEnvFile).toHaveBeenCalledWith('USE_DOCKER', 'NO'); | ||
}); | ||
|
||
it('should handle errors when askForDocker fails', async () => { | ||
(inquirer.prompt as unknown as jest.Mock).mockResolvedValueOnce({ | ||
useDocker: true, | ||
}); | ||
(askForDocker as jest.Mock).mockRejectedValueOnce( | ||
new Error('Docker error'), | ||
); | ||
|
||
await expect(askAndSetDockerOption()).rejects.toThrow('Docker error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import inquirer from 'inquirer'; | ||
import updateEnvFile from 'setup/updateEnvFile/updateEnvFile'; | ||
import { askForDocker } from 'setup/askForDocker/askForDocker'; | ||
|
||
// Function to manage Docker setup | ||
const askAndSetDockerOption = async (): Promise<void> => { | ||
const { useDocker } = await inquirer.prompt({ | ||
type: 'confirm', | ||
name: 'useDocker', | ||
message: 'Would you like to set up with Docker?', | ||
default: false, | ||
}); | ||
|
||
if (useDocker) { | ||
console.log('Setting up with Docker...'); | ||
updateEnvFile('USE_DOCKER', 'YES'); | ||
const answers = await askForDocker(); | ||
const DOCKER_PORT_NUMBER = answers; | ||
updateEnvFile('DOCKER_PORT', DOCKER_PORT_NUMBER); | ||
|
||
const DOCKER_NAME = 'talawa-admin'; | ||
console.log(` | ||
Run the commands below after setup:- | ||
1. docker build -t ${DOCKER_NAME} . | ||
2. docker run -d -p ${DOCKER_PORT_NUMBER}:${DOCKER_PORT_NUMBER} ${DOCKER_NAME} | ||
`); | ||
} else { | ||
console.log('Setting up without Docker...'); | ||
updateEnvFile('USE_DOCKER', 'NO'); | ||
} | ||
}; | ||
|
||
export default askAndSetDockerOption; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import updateEnvFile from 'setup/updateEnvFile/updateEnvFile'; | ||
import { askForCustomPort } from 'setup/askForCustomPort/askForCustomPort'; | ||
import inquirer from 'inquirer'; | ||
|
||
// Ask and update the custom port | ||
const askAndUpdatePort = async (): Promise<void> => { | ||
const { shouldSetCustomPortResponse } = await inquirer.prompt({ | ||
type: 'confirm', | ||
name: 'shouldSetCustomPortResponse', | ||
message: | ||
'Would you like to set up a custom port for running Talawa Admin without Docker?', | ||
default: true, | ||
}); | ||
|
||
if (shouldSetCustomPortResponse) { | ||
const customPort = await askForCustomPort(); | ||
if (customPort < 1024 || customPort > 65535) { | ||
throw new Error('Port must be between 1024 and 65535'); | ||
} | ||
|
||
updateEnvFile('PORT', String(customPort)); | ||
} | ||
}; | ||
|
||
export default askAndUpdatePort; |
Oops, something went wrong.