Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/main/index.js.map

Large diffs are not rendered by default.

41 changes: 34 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,28 @@ export async function downloadUpdateInstaller(config: VersionConfig): Promise<st
return joinPaths(toolPath, 'sqlupdate.exe');
}

/**
* Escape the glob characters in a literal path, so that a glob pattern matches it exactly. On Windows, @actions/glob
* has no escape character, so wrap each glob character in [].
*
* @param {string} path
* @returns {string}
*/
function escapeGlob(path: string): string {
return path.replace(/[[*?]/g, '[$&]');
}

/**
* The glob pattern of the SQL Server directory in Program Files, where setup writes its logs and installs instances
* by default. The Program Files directory can be on a drive other than C:, so use the ProgramFiles environment
* variable.
*
* @returns {string}
*/
function sqlServerDirPattern(): string {
return win32.join(escapeGlob(process.env.ProgramFiles || 'C:\\Program Files'), 'Microsoft SQL Server');
}

/**
* Gather installation summary file. Used after installation to output summary data.
* Optionally can also fetch the Detail.txt file (useful for debugging errors).
Expand All @@ -466,19 +488,23 @@ export async function downloadUpdateInstaller(config: VersionConfig): Promise<st
export async function gatherSummaryFiles(withDetail: boolean = false): Promise<string[]> {
// The summary file is in a different location depending on the version of SQL Server being installed,
// use glob to find it.
const summaryFiles = await glob.create('C:/Program Files/Microsoft SQL Server/[0-9]*/Setup Bootstrap/Log/Summary.txt')
const logDir = win32.join(sqlServerDirPattern(), '[0-9]*', 'Setup Bootstrap', 'Log');
const summaryFiles = await glob.create(win32.join(logDir, 'Summary.txt'))
.then((globber) => globber.glob());
if (summaryFiles.length) {
core.debug(`Found files: ${summaryFiles.join(', ')}`);
} else {
core.notice('No summary files found');
}
// try to find detail file, this is in a directory that contains the installation date_time
// sort the files and then pull the last one off (as that is likely to be the most recent
// installation attempt)
const detailFile = withDetail ? await glob.create('C:/Program Files/Microsoft SQL Server/[0-9]*/Setup Bootstrap/Log/[0-9]*_[0-9]*/Detail.txt')
// Setup writes the Detail.txt of each run in a directory named after the start time of the run (YYYYMMDD_HHMMSS).
// Each SQL Server version has its own log directory. Sort the files by the start time and use the last file, which
// is from the most recent run. Do not sort by the full path: the version directory comes first in the path, so
// that sort chooses an older run of a later version. If two runs have the same start time, use the full path.
const startTime = (file: string) => win32.basename(win32.dirname(file));
const compare = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
const detailFile = withDetail ? await glob.create(win32.join(logDir, '[0-9]*_[0-9]*', 'Detail.txt'))
.then((globber) => globber.glob())
.then((files) => files.sort())
.then((files) => files.sort((a, b) => compare(startTime(a), startTime(b)) || compare(a, b)))
.then((files) => {
if (files.length) {
core.debug(`Found detail files: ${files.join(', ')}`);
Expand Down Expand Up @@ -512,7 +538,8 @@ export async function gatherErrorLogs(instanceName: string): Promise<string[]> {
.filter((file) => file.startsWith(logName) && /^(\.\d+)?$/.test(file.slice(logName.length)))
.map((file) => win32.join(logDirectory, file));
} else {
errorLogs = await glob.create(`C:/Program Files/Microsoft SQL Server/MSSQL[0-9]*.${instanceName}/MSSQL/Log/ERRORLOG*`)
const instanceDir = `MSSQL[0-9]*.${escapeGlob(instanceName)}`;
errorLogs = await glob.create(win32.join(sqlServerDirPattern(), instanceDir, 'MSSQL', 'Log', 'ERRORLOG*'))
.then((globber) => globber.glob());
}
if (errorLogs.length) {
Expand Down
82 changes: 80 additions & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ mock.module('../src/crypto.ts', { namedExports: { generateFileHash } });

const utils = await import('../src/utils.ts');

/**
* Set the ProgramFiles environment variable for a suite's tests, and restore it after each one.
*/
function useProgramFiles(value: string) {
const original = process.env.ProgramFiles;
beforeEach(() => {
process.env.ProgramFiles = value;
});
afterEach(() => {
if (original === undefined) {
delete process.env.ProgramFiles;
} else {
process.env.ProgramFiles = original;
}
});
}

function resetAll() {
const fns = [
core.getInput, core.getMultilineInput, core.getBooleanInput, core.setSecret,
Expand Down Expand Up @@ -888,11 +905,41 @@ describe('utils', () => {
});
});
describe('.gatherSummaryFiles()', () => {
const logDir = 'C:\\Program Files\\Microsoft SQL Server\\[0-9]*\\Setup Bootstrap\\Log';
let globFn: ReturnType<typeof mock.fn>;
useProgramFiles('C:\\Program Files');
beforeEach(() => {
globFn = mock.fn(async () => [] as string[]);
globCreate.mock.mockImplementation(async () => ({ glob: globFn } as any));
});
it('searches the setup logs in the Program Files directory', async () => {
await utils.gatherSummaryFiles(true);
assert.deepEqual(globCreate.mock.calls.map((c) => c.arguments[0]), [
`${logDir}\\Summary.txt`,
`${logDir}\\[0-9]*_[0-9]*\\Detail.txt`,
]);
});
it('searches the setup logs on the drive of the Program Files directory', async () => {
process.env.ProgramFiles = 'E:\\Program Files';
await utils.gatherSummaryFiles(true);
assert.deepEqual(globCreate.mock.calls.map((c) => c.arguments[0]), [
'E:\\Program Files\\Microsoft SQL Server\\[0-9]*\\Setup Bootstrap\\Log\\Summary.txt',
'E:\\Program Files\\Microsoft SQL Server\\[0-9]*\\Setup Bootstrap\\Log\\[0-9]*_[0-9]*\\Detail.txt',
]);
});
it('searches C:\\Program Files if the Program Files directory is not set', async () => {
delete process.env.ProgramFiles;
await utils.gatherSummaryFiles();
assert.equal(globCreate.mock.calls[0].arguments[0], `${logDir}\\Summary.txt`);
});
it('escapes glob characters in the Program Files directory', async () => {
process.env.ProgramFiles = 'D:\\Apps [x64]*?';
await utils.gatherSummaryFiles(true);
assert.deepEqual(globCreate.mock.calls.map((c) => c.arguments[0]), [
'D:\\Apps [[]x64][*][?]\\Microsoft SQL Server\\[0-9]*\\Setup Bootstrap\\Log\\Summary.txt',
'D:\\Apps [[]x64][*][?]\\Microsoft SQL Server\\[0-9]*\\Setup Bootstrap\\Log\\[0-9]*_[0-9]*\\Detail.txt',
]);
});
it('returns empty array if no files matched', async () => {
const res = await utils.gatherSummaryFiles();
assert.deepEqual(res, []);
Expand All @@ -916,9 +963,31 @@ describe('utils', () => {
assert.deepEqual(res, ['C:/tmp/summary.txt', 'C:/tmp/2022/details.txt']);
assert.equal(globCreate.mock.callCount(), 2);
});
const detail = (version: string, run: string) => `C:\\Program Files\\Microsoft SQL Server\\${version}\\Setup Bootstrap\\Log\\${run}\\Detail.txt`;
it('finds the detail file of the most recent setup run of any version', async () => {
globFn.mock.mockImplementationOnce(async () => [], 0);
globFn.mock.mockImplementationOnce(async () => [
detail('150', '20250102_090000'),
detail('160', '20250101_120000'),
detail('160', '20250301_080000'),
detail('170', '20240615_100000'),
], 1);
const res = await utils.gatherSummaryFiles(true);
assert.deepEqual(res, [detail('160', '20250301_080000')]);
});
for (const order of ['in order', 'in reverse order']) {
it(`chooses by the full path if two runs have the same start time, ${order}`, async () => {
const files = [detail('150', '20250301_080000'), detail('160', '20250301_080000')];
globFn.mock.mockImplementationOnce(async () => [], 0);
globFn.mock.mockImplementationOnce(async () => (order === 'in order' ? files : files.toReversed()), 1);
const res = await utils.gatherSummaryFiles(true);
assert.deepEqual(res, [detail('160', '20250301_080000')]);
});
}
});
describe('.gatherErrorLogs()', () => {
let globFn: ReturnType<typeof mock.fn>;
useProgramFiles('C:\\Program Files');
beforeEach(() => {
globFn = mock.fn(async () => [] as string[]);
globCreate.mock.mockImplementation(async () => ({ glob: globFn } as any));
Expand All @@ -932,7 +1001,16 @@ describe('utils', () => {
it('searches the log directory of the instance', async () => {
await utils.gatherErrorLogs('MSSQLSERVER');
assert.equal(globCreate.mock.callCount(), 1);
assert.equal(globCreate.mock.calls[0].arguments[0], 'C:/Program Files/Microsoft SQL Server/MSSQL[0-9]*.MSSQLSERVER/MSSQL/Log/ERRORLOG*');
assert.equal(globCreate.mock.calls[0].arguments[0], 'C:\\Program Files\\Microsoft SQL Server\\MSSQL[0-9]*.MSSQLSERVER\\MSSQL\\Log\\ERRORLOG*');
});
it('escapes glob characters in the instance name', async () => {
await utils.gatherErrorLogs('A[1]*?');
assert.equal(globCreate.mock.calls[0].arguments[0], 'C:\\Program Files\\Microsoft SQL Server\\MSSQL[0-9]*.A[[]1][*][?]\\MSSQL\\Log\\ERRORLOG*');
});
it('searches the instance directory on the drive of the Program Files directory', async () => {
process.env.ProgramFiles = 'E:\\Program Files';
await utils.gatherErrorLogs('MSSQLSERVER');
assert.equal(globCreate.mock.calls[0].arguments[0], 'E:\\Program Files\\Microsoft SQL Server\\MSSQL[0-9]*.MSSQLSERVER\\MSSQL\\Log\\ERRORLOG*');
});
it('returns found files oldest first', async () => {
const dir = 'C:/Program Files/Microsoft SQL Server/MSSQL16.MSSQLSERVER/MSSQL/Log';
Expand Down Expand Up @@ -1014,7 +1092,7 @@ describe('utils', () => {
assert.deepEqual(res, []);
assert.equal(exec.getExecOutput.mock.callCount(), 1);
assert.equal(globCreate.mock.callCount(), 1);
assert.equal(globCreate.mock.calls[0].arguments[0], 'C:/Program Files/Microsoft SQL Server/MSSQL[0-9]*.OTHER/MSSQL/Log/ERRORLOG*');
assert.equal(globCreate.mock.calls[0].arguments[0], 'C:\\Program Files\\Microsoft SQL Server\\MSSQL[0-9]*.OTHER\\MSSQL\\Log\\ERRORLOG*');
});
it('uses the default location if the startup parameters have no error log', async () => {
parameters = parameters.replace(/^.*SQLArg1.*$/m, '');
Expand Down
Loading