Skip to content

Commit

Permalink
Merge pull request #62 from emilkrebs/templateTesting
Browse files Browse the repository at this point in the history
Template testing
  • Loading branch information
emilkrebs authored Nov 17, 2023
2 parents bd9f7b0 + 020d788 commit 54045d1
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 129 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"dotenv": "^16.3.1",
"eslint": "^8.53.0",
"typescript": "^5.2.2",
"vitest": "^0.34.6",
Expand Down
4 changes: 2 additions & 2 deletions templates/python/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import discord

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

Expand All @@ -17,4 +16,5 @@ async def on_message(message):
if message.content.startswith('!ping'):
await message.channel.send('Pong!')

client.run('<%= bot-token %>')
# 'log_handler=None' is used to disable the default logging handler. Remove it if you want to use the default logging handler.
client.run('<%= bot-token %>', log_handler=None)
127 changes: 0 additions & 127 deletions test/generator.test.ts

This file was deleted.

105 changes: 105 additions & 0 deletions test/templates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { describe, expect, test } from 'vitest';
import { createHelpers } from 'yeoman-test';
import { spawn, exec } from 'child_process';
import * as dotenv from 'dotenv';
import path from 'path';
import url from 'url';

const __dirname = url.fileURLToPath(new URL('../', import.meta.url));
const TIMEOUT = 30_000; // 30 seconds

dotenv.config({ path: path.join(__dirname, '.env') });

const { BOT_TOKEN } = process.env;
if (!BOT_TOKEN) {
throw new Error('You must specify a valid bot token in the BOT_TOKEN environment variable');
}

const defaultAnswers = {
botName: 'test-run-bot',
botType: 'typescript',
botToken: BOT_TOKEN,
openWithCode: false,
};

describe('Check if the templates work', () => {
const targetRoot = path.join(__dirname, 'test-temp');
const moduleRoot = path.join(__dirname, 'app');
const resultRoot = path.join(targetRoot, defaultAnswers.botName);

test('Should generate and run TypeScript Discord Bot', async () => {
const context = createHelpers({}).run(moduleRoot);

context.targetDirectory = targetRoot;
context.cleanTestDirectory(true);
await context
.onGenerator(generator => {
generator.destinationRoot(targetRoot);
})
.withAnswers(defaultAnswers)
.then(async () => {
const result = await runBot('node', ['./dist/src/index.js'], resultRoot);
expect(result).toContain(BOT_OUTPUT_START);
});

context.cleanup();
}, 120_000);


test.skip('Should generate and run Python Discord Bot', async () => {
const context = createHelpers({}).run(moduleRoot);

context.targetDirectory = targetRoot;
context.cleanTestDirectory(true);
await context
.onGenerator(generator => {
generator.destinationRoot(targetRoot);
})
.withAnswers({...defaultAnswers, botType: 'python' })
.then(async () => {
// install dependencies
await exec('pip3 install -r requirements.txt', { cwd: resultRoot });

const result = await runBot('python', ['main.py'], resultRoot);
expect(result).toContain(BOT_OUTPUT_START);
});

context.cleanup();
}, 120_000);
});


async function runBot(command: string, args: string[], root: string): Promise<string> {
const childProcess = spawn(command, args, { cwd: root, stdio: 'pipe', timeout: TIMEOUT });

const result = await new Promise<string>((resolve) => {
// timeout
const timeout = setTimeout(() => {
resolve('TIMEOUT');
}, TIMEOUT);

// output
childProcess.stdout.on('data', (data) => {
clearTimeout(timeout);

resolve(data.toString());
});

childProcess.stderr.on('data', (data) => {
clearTimeout(timeout);

resolve(data.toString());
});

childProcess.on('close', () => {
clearTimeout(timeout);
});

});

childProcess.kill('SIGINT');
return result;
}


const BOT_OUTPUT_START = 'Bot is logged in as';

0 comments on commit 54045d1

Please sign in to comment.