import { Eltro as t, assert, stub } from 'eltro' import Util from '../../core/util.mjs' import fs from 'fs/promises' import GitProvider from '../../core/providers/git.mjs' t.timeout(5000).describe('Git integration', function() { t.after(function() { return fs.rm('./test/providers/file.7z') .catch(function() { }) }) t.describe('#getLatestVersion()', function() { t.test('should return latest version in a valid repository', async function() { let provider = new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/thething/sc-helloworld/releases' }) let version = await provider.getLatestVersion() assert.ok(version) assert.ok(version.version) assert.ok(version.description) assert.ok(version.link) assert.match(version.link, /\/attachments\//) }) }) t.describe('#checkConfig()', function() { t.test('should fail if link does not return json repository object', async function() { let err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/repos/thething/ProgramQueuer' }).checkConfig()) assert.match(err.message, /valid/i) assert.match(err.message, /repository/i) err = await assert.isRejected(new GitProvider({ url: 'http://git.nfp.is/api/v1/orgs/nfp/repos' }).checkConfig()) assert.match(err.message, /service-core/i) assert.match(err.message, /release/i) }) t.test('should fail if link returns no active release repository with assets', async function() { let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/thething/eltro/releases' }).checkConfig()) assert.match(err.message, /service-core/i) assert.match(err.message, /release/i) }) t.test('should fail on private repositories if token missing', async function() { let err = await assert.isRejected(new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases' }).checkConfig()) assert.match(err.message, /fail/i) assert.match(err.message, /404/i) assert.match(err.message, /release/i) }) t.test('should otherwise succeed', function() { return new GitProvider({ url: 'https://git.nfp.is/api/v1/repos/TheThing/sc-manager/releases' }) .checkConfig() }) let test = t if (!process.env.gittesttoken) { test = test.skip() } test.test('should succeed on private repo if token is provided', function() { return new GitProvider({ token: process.env.gittesttoken.trim(), url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases', }).checkConfig() }) }) t.describe('#downloadVersion()', function() { const util = new Util(import.meta.url) let test = t if (!process.env.gittesttoken) { test = test.skip() } test.test('should successfully download release', async function() { let provider = new GitProvider({ token: process.env.gittesttoken.trim(), url: 'https://git.nfp.is/api/v1/repos/TheThing/privateexample/releases', }) await provider.checkConfig() let version = await provider.getLatestVersion() assert.ok(version.version) assert.ok(version.filename) assert.ok(version.link) let path = util.getPathFromRoot('./file.7z') await provider.downloadVersion(version, path) let stat = await fs.stat(path) assert.ok(stat.size > 0) stat = await fs.stat('./test/providers/file.7z') assert.ok(stat.size > 0) let output = '' await util.runCommand(util.get7zipExecutable(), ['l', 'file.7z'], util.getPathFromRoot('./'), function(chunk) { output += chunk + '\n' }) assert.ok(output.indexOf('file1.txt')) assert.ok(output.indexOf('file2.txt')) }) }) })