diff --git a/src/commands/logs/build.ts b/src/commands/logs/build.ts index 502d1b97d92..d16fdf43bd4 100644 --- a/src/commands/logs/build.ts +++ b/src/commands/logs/build.ts @@ -39,6 +39,11 @@ export const logsBuild = async (options: OptionValues, command: BaseCommand) => const { id: siteId } = site const userId = command.netlify.globalConfig.get('userId') + if (!siteId) { + log('You must link a site before attempting to view deploy logs') + return + } + const deploys = await client.listSiteDeploys({ siteId, state: 'building' }) if (deploys.length === 0) { diff --git a/tests/integration/commands/logs/build.test.ts b/tests/integration/commands/logs/build.test.ts index dc1e654f039..956bab228a4 100644 --- a/tests/integration/commands/logs/build.test.ts +++ b/tests/integration/commands/logs/build.test.ts @@ -5,6 +5,10 @@ import { createLogsBuildCommand } from '../../../../src/commands/logs/index.js' import { getWebSocket } from '../../../../src/utils/websockets/index.js' import { startMockApi } from '../../utils/mock-api-vitest.js' import { getEnvironmentVariables } from '../../utils/mock-api.js' +import { callCli } from '../../utils/call-cli.js' +import { getCLIOptions, withMockApi } from '../../utils/mock-api.js' +import { withSiteBuilder } from '../../utils/site-builder.ts' +import { join } from 'path' vi.mock('../../../../src/utils/websockets/index.js', () => ({ getWebSocket: vi.fn(), @@ -47,12 +51,14 @@ const routes = [ describe('logs:deploy command', () => { let program: BaseCommand + const originalEnv = { ...process.env } afterEach(() => { vi.clearAllMocks() }) beforeEach(() => { + process.env = { ...originalEnv } program = new BaseCommand('netlify') createLogsBuildCommand(program) @@ -107,4 +113,21 @@ describe('logs:deploy command', () => { expect(body.site_id).toEqual('site_id') expect(body.access_token).toEqual(env.NETLIFY_AUTH_TOKEN) }) + + test('should instruct user to link a site if one is not linked', async (t) => { + await withSiteBuilder(t, async (builder) => { + const projectPath = join('projects', 'project1') + await builder.withNetlifyToml({ config: {}, pathPrefix: projectPath }).build() + + await withMockApi( + routes, + async ({ apiUrl }) => { + const options = getCLIOptions({ builder, apiUrl, env: { NETLIFY_SITE_ID: '' } }) + const stdout = await callCli(['logs:deploy'], { ...options, cwd: join(builder.directory, projectPath) }) + expect(stdout).toContain('You must link a site before attempting to view deploy logs') + }, + true, + ) + }) + }) })