Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #177 from github/external_queries_test
Make a local repo for checkoutExternalQueries
Robert authored and GitHub committed Sep 10, 2020

Unverified

No user is associated with the committer email.
2 parents c5f77d0 + d4f40db commit 75af0bf
Showing 6 changed files with 147 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/external-queries.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/external-queries.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 65 additions & 7 deletions lib/external-queries.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/external-queries.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 78 additions & 6 deletions src/external-queries.test.ts
@@ -1,3 +1,4 @@
import * as toolrunnner from '@actions/exec/lib/toolrunner';
import test from 'ava';
import * as fs from "fs";
import * as path from "path";
@@ -11,15 +12,86 @@ setupTests(test);

test("checkoutExternalQueries", async t => {
await util.withTmpDir(async tmpDir => {
const ref = "df4c6869212341b601005567381944ed90906b6b";
// Create a test repo in a subdir of the temp dir.
// It should have a default branch with two commits after the initial commit, where
// - the first commit contains files 'a' and 'b'
// - the second commit contains only 'a'
// Place the repo in a subdir because we're going to checkout a copy in tmpDir
const testRepoBaseDir = path.join(tmpDir, 'test-repo-dir');
const repoName = 'some/repo';
const repoPath = path.join(testRepoBaseDir, repoName);
const repoGitDir = path.join(repoPath, '.git');

// Run the given git command, and return the output.
// Passes --git-dir and --work-tree.
// Any stderr output is suppressed until the command fails.
const runGit = async function(command: string[]): Promise<string> {
let stdout = '';
let stderr = '';
command = [`--git-dir=${repoGitDir}`, `--work-tree=${repoPath}`, ...command];
console.log('Running: git ' + command.join(' '));
try {
await new toolrunnner.ToolRunner('git', command, {
silent: true,
listeners: {
stdout: (data) => { stdout += data.toString(); },
stderr: (data) => { stderr += data.toString(); },
}
}).exec();
} catch (e) {
console.log('Command failed: git ' + command.join(' '));
process.stderr.write(stderr);
throw e;
}
return stdout.trim();
};

fs.mkdirSync(repoPath, { recursive: true });
await runGit(['init', repoPath]);
await runGit(['config', 'user.email', 'test@github.com']);
await runGit(['config', 'user.name', 'Test Test']);

fs.writeFileSync(path.join(repoPath, 'a'), 'a content');
await runGit(['add', 'a']);
await runGit(['commit', '-m', 'commit1']);

fs.writeFileSync(path.join(repoPath, 'b'), 'b content');
await runGit(['add', 'b']);
await runGit(['commit', '-m', 'commit1']);
const commit1Sha = await runGit(['rev-parse', 'HEAD']);

fs.unlinkSync(path.join(repoPath, 'b'));
await runGit(['add', 'b']);
await runGit(['commit', '-m', 'commit2']);
const commit2Sha = await runGit(['rev-parse', 'HEAD']);



// Checkout the first commit, which should contain 'a' and 'b'
t.false(fs.existsSync(path.join(tmpDir, repoName)));
await externalQueries.checkoutExternalRepository(
"github/codeql-go",
ref,
'https://github.com',
repoName,
commit1Sha,
`file://${testRepoBaseDir}`,
tmpDir,
getRunnerLogger(true));
t.true(fs.existsSync(path.join(tmpDir, repoName)));
t.true(fs.existsSync(path.join(tmpDir, repoName, commit1Sha)));
t.true(fs.existsSync(path.join(tmpDir, repoName, commit1Sha, 'a')));
t.true(fs.existsSync(path.join(tmpDir, repoName, commit1Sha, 'b')));



// COPYRIGHT file existed in df4c6869212341b601005567381944ed90906b6b but not in the default branch
t.true(fs.existsSync(path.join(tmpDir, "github", "codeql-go", ref, "COPYRIGHT")));
// Checkout the second commit as well, which should only contain 'a'
t.false(fs.existsSync(path.join(tmpDir, repoName, commit2Sha)));
await externalQueries.checkoutExternalRepository(
repoName,
commit2Sha,
`file://${testRepoBaseDir}`,
tmpDir,
getRunnerLogger(true));
t.true(fs.existsSync(path.join(tmpDir, repoName, commit2Sha)));
t.true(fs.existsSync(path.join(tmpDir, repoName, commit2Sha, 'a')));
t.false(fs.existsSync(path.join(tmpDir, repoName, commit2Sha, 'b')));
});
});
2 changes: 1 addition & 1 deletion src/external-queries.ts
@@ -24,7 +24,7 @@ export async function checkoutExternalRepository(
}

if (!fs.existsSync(checkoutLocation)) {
const repoURL = githubUrl + '/' + repository + '.git';
const repoURL = githubUrl + '/' + repository;
await new toolrunnner.ToolRunner('git', ['clone', repoURL, checkoutLocation]).exec();
await new toolrunnner.ToolRunner('git', [
'--work-tree=' + checkoutLocation,

0 comments on commit 75af0bf

Please sign in to comment.