1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(cli): add --all option to run test command

This commit is contained in:
Divlo 2021-10-13 21:43:45 +02:00
parent aeff95e691
commit 0a27b9e1a1
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
47 changed files with 1657 additions and 1773 deletions

View File

@ -45,7 +45,7 @@ Gitpod will automatically setup an environment for you.
#### Prerequisites
- [Node.js](https://nodejs.org/) >= 16.0.0
- [npm](https://npmjs.com/) >= 7.0.0
- [npm](https://npmjs.com/) >= 8.0.0
- [Docker](https://www.docker.com/)
#### Installation

View File

@ -9,7 +9,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
const char character_to_upper(const char character) {
char character_to_upper(char character) {
char ascii_a = 'a';
char ascii_A = 'A';
char ascii_z = 'z';

View File

@ -1,5 +1,5 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
@ -14,8 +14,8 @@ void character_append(char* string, char character);
* @brief Converts the character to uppercase.
*
* @param character
* @return const char
* @return char
*/
const char character_to_upper(const char character);
char character_to_upper(char character);
#endif

View File

@ -1,5 +1,5 @@
#ifndef INPUT_H
#define INPUT_H
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.

View File

@ -1,5 +1,5 @@
#ifndef STRING_H
#define STRING_H
#ifndef __STRING__
#define __STRING__
/**
* @brief Converts all the alphabetic characters in a string to uppercase.

View File

@ -29,7 +29,7 @@ char* string_caesar_cipher(char* string, int shift) {
for (size_t index = 0; index < string_length; index++) {
char letter = string[index];
if (letter != ' ') {
for (char index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
for (int index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
char current_letter = 'A' + index_alphabet;
if (string[index] == current_letter) {
letter = shifted_alphabet[index_alphabet];

View File

@ -9,7 +9,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
const char character_to_upper(const char character) {
char character_to_upper(char character) {
char ascii_a = 'a';
char ascii_A = 'A';
char ascii_z = 'z';

View File

@ -14,8 +14,8 @@ void character_append(char* string, char character);
* @brief Converts the character to uppercase.
*
* @param character
* @return const char
* @return char
*/
const char character_to_upper(const char character);
char character_to_upper(char character);
#endif

View File

@ -42,8 +42,8 @@ int main() {
if (is_last_number) {
break;
}
if (numbers[index] + couple_index == numbers[index + couple_index]) {
consecutive[consecutive_length] = numbers[index] + couple_index;
if ((int)(numbers[index] + couple_index) == numbers[index + couple_index]) {
consecutive[consecutive_length] = (int)(numbers[index] + couple_index);
consecutive_length++;
}
bool is_consecutive = consecutive_length == couple_length;

View File

@ -0,0 +1,10 @@
#include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) {
size_t length = strlen(string);
string[length] = character;
string[length + 1] = '\0';
}

View File

@ -0,0 +1,13 @@
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
* with enough space.
*
* @param string
* @param character
*/
void character_append(char* string, char character);
#endif

View File

@ -0,0 +1,19 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() {
char character;
size_t length = 1;
char* string = malloc(length * sizeof(char));
*string = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string = realloc(string, length * sizeof(char));
character_append(string, character);
}
return string;
}

View File

@ -0,0 +1,11 @@
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "input.h"
int main() {
char input[1024];
while (scanf("%s", &input) != EOF) {
printf("Hello, %s!", input);
}
char *string = input();
printf("Hello, %s!\n", string);
return EXIT_SUCCESS;
}

View File

@ -9,7 +9,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
const char character_to_upper(const char character) {
char character_to_upper(char character) {
char ascii_a = 'a';
char ascii_A = 'A';
char ascii_z = 'z';

View File

@ -1,5 +1,5 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
@ -14,8 +14,8 @@ void character_append(char* string, char character);
* @brief Converts the character to uppercase.
*
* @param character
* @return const char
* @return char
*/
const char character_to_upper(const char character);
char character_to_upper(char character);
#endif

View File

@ -1,6 +1,7 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"

View File

@ -1,5 +1,5 @@
#ifndef INPUT_H
#define INPUT_H
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.

View File

@ -18,7 +18,7 @@ char* string_to_upper(const char* string) {
char* string_reverse(const char* string) {
size_t string_length = strlen(string);
char* result = malloc(sizeof(char) * (string_length + 1));
for (size_t index = string_length - 1; index != -1; index--) {
for (int index = string_length - 1; index != -1; index--) {
character_append(result, string[index]);
}
return result;

View File

@ -1,5 +1,5 @@
#ifndef STRING_H
#define STRING_H
#ifndef __STRING__
#define __STRING__
#include <stdbool.h>

View File

@ -10,7 +10,7 @@ void character_append(char* string, char character) {
string[length + 1] = '\0';
}
void character_print(char* character, int number_of_times) {
void character_print(char* character, size_t number_of_times) {
for (size_t iteration = 0; iteration < number_of_times; iteration++) {
printf("%s", character);
}

View File

@ -1,5 +1,7 @@
#ifndef CHARACTER_H
#define CHARACTER_H
#ifndef __CHARACTER__
#define __CHARACTER__
#include <stdlib.h>
/**
* @brief Append a character to a string, assuming string points to an array
@ -10,6 +12,6 @@
*/
void character_append(char* string, char character);
void character_print(char* character, int number_of_times);
void character_print(char* character, size_t number_of_times);
#endif

View File

@ -1,5 +1,5 @@
#ifndef INPUT_H
#define INPUT_H
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.

View File

@ -12,10 +12,10 @@ int main() {
int step = strcmp(type, "normal") == 0 ? 1 : height;
while ((strcmp(type, "normal") == 0 && step <= height) || (strcmp(type, "reverse") == 0 && step != 0)) {
int numberOfStars = (step * 2) - 1;
int totalNumberOfLocations = (height * 2) - 1;
int totalNumberOfSpaces = totalNumberOfLocations - numberOfStars;
int numberOfSpacesOnEachSide = totalNumberOfSpaces / 2;
size_t numberOfStars = (step * 2) - 1;
size_t totalNumberOfLocations = (height * 2) - 1;
size_t totalNumberOfSpaces = totalNumberOfLocations - numberOfStars;
size_t numberOfSpacesOnEachSide = totalNumberOfSpaces / 2;
character_print(" ", numberOfSpacesOnEachSide);
character_print("*", numberOfStars);
character_print(" ", numberOfSpacesOnEachSide);

View File

@ -1,5 +1,5 @@
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
#ifndef __BUBBLE_SORT__
#define __BUBBLE_SORT__
void swap_numbers(int *value_1, int *value_2);

View File

@ -5,7 +5,7 @@
int main() {
int current_number;
int length = scanf("%d", &current_number);
scanf("%d", &current_number);
int *numbers = malloc(current_number * sizeof(int));
int index_input = 0;
while (scanf("%d", &current_number) != EOF) {

View File

@ -1,5 +1,5 @@
#ifndef INSERTION_SORT_H
#define INSERTION_SORT_H
#ifndef __INSERTION_SORT__
#define __INSERTION_SORT__
void insertion_sort(int *numbers, const int length);

View File

@ -5,7 +5,7 @@
int main() {
int current_number;
int length = scanf("%d", &current_number);
scanf("%d", &current_number);
int *numbers = malloc(current_number * sizeof(int));
int index_input = 0;
while (scanf("%d", &current_number) != EOF) {

View File

@ -1,5 +1,5 @@
#ifndef MERGE_SORT_H
#define MERGE_SORT_H
#ifndef __MERGE_SORT__
#define __MERGE_SORT__
void merge(int numbers[], int left_index, int middle_index, int right_index);

View File

@ -5,7 +5,7 @@
int main() {
int current_number;
int length = scanf("%d", &current_number);
scanf("%d", &current_number);
int *numbers = malloc(current_number * sizeof(int));
int index_input = 0;
while (scanf("%d", &current_number) != EOF) {

View File

@ -5,15 +5,12 @@ char* triangle_type(int triangle_sides[3]) {
if ((triangle_sides[0] + triangle_sides[1] < triangle_sides[2]) || (triangle_sides[2] + triangle_sides[0] < triangle_sides[1]) || (triangle_sides[2] + triangle_sides[1] < triangle_sides[0])) {
return "impossible";
}
if (triangle_sides[0] == triangle_sides[1] && triangle_sides[1] == triangle_sides[2]) {
return "equilateral";
}
if (triangle_sides[0] == triangle_sides[1] || triangle_sides[1] == triangle_sides[2] || triangle_sides[2] == triangle_sides[0]) {
return "isosceles";
}
return "scalene";
}

View File

@ -1,9 +1,13 @@
import fs from 'node:fs'
import path from 'node:path'
import { Command, Option } from 'clipanion'
import * as typanion from 'typanion'
import chalk from 'chalk'
import { Solution } from '../../services/Solution'
import { GitAffected } from '../../services/GitAffected'
import { template } from '../../services/Template'
const successMessage = `${chalk.bold.green('Success:')} Tests passed! 🎉`
@ -16,7 +20,7 @@ export class RunTestCommand extends Command {
}
public programmingLanguage = Option.String('--language', {
description: 'The programming language to use to solve the challenge.',
description: 'The programming language used to solve the challenge.',
validator: typanion.isString()
})
@ -34,6 +38,10 @@ export class RunTestCommand extends Command {
description: 'Only run the tests for the affected files in `git`.'
})
public all = Option.Boolean('--all', false, {
description: 'Run the tests for all the solutions.'
})
public isContinuousIntegration = Option.Boolean('--ci', false, {
description: 'Run the tests for the Continuous Integration (CI).'
})
@ -42,15 +50,7 @@ export class RunTestCommand extends Command {
description: 'Base of the current branch (usually master)'
})
async execute (): Promise<number> {
console.log()
try {
if (this.affected) {
const gitAffected = new GitAffected({
isContinuousIntegration: this.isContinuousIntegration,
base: this.base
})
const solutions = await gitAffected.getAffectedSolutions()
async runTests (solutions: Solution[]): Promise<number> {
for (const solution of solutions) {
await solution.test()
console.log('\n------------------------------\n')
@ -58,6 +58,52 @@ export class RunTestCommand extends Command {
console.log(successMessage)
return 0
}
async execute (): Promise<number> {
console.log()
try {
if (this.programmingLanguage != null) {
await template.verifySupportedProgrammingLanguage(this.programmingLanguage)
}
if (this.all) {
const challengesPath = path.join(
__dirname,
'..',
'..',
'..',
'challenges'
)
const challenges = await fs.promises.readdir(challengesPath)
const paths: string[] = []
for (const challenge of challenges) {
const solutionsPath = path.join(challengesPath, challenge, 'solutions')
const languagesSolution = (await fs.promises.readdir(solutionsPath)).filter(
(name) => {
if (this.programmingLanguage != null) {
return name === this.programmingLanguage
}
return name !== '.gitkeep'
}
)
for (const language of languagesSolution) {
const solutionPath = (await fs.promises.readdir(path.join(solutionsPath, language))).map((solutionName) => {
return `challenges/${challenge}/solutions/${language}/${solutionName}`
})
paths.push(...solutionPath)
}
}
const solutions = await Solution.getManyByPaths(paths)
await this.runTests(solutions)
return 0
}
if (this.affected) {
const gitAffected = new GitAffected({
isContinuousIntegration: this.isContinuousIntegration,
base: this.base
})
const solutions = await gitAffected.getAffectedSolutions()
return await this.runTests(solutions)
}
if (
this.solutionName == null ||
this.challenge == null ||

View File

@ -1,8 +1,6 @@
import execa from 'execa'
import { Challenge } from './Challenge'
import { Solution } from './Solution'
import { isExistingPath } from '../utils/isExistingPath'
const solutionsRegex = new RegExp(
/challenges\/[\s\S]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\s\S]*\/(solution|Solution).(c|cpp|cs|dart|java|js|py|rs|ts)/
@ -72,22 +70,6 @@ export class GitAffected implements GitAffectedOptions {
const affectedSolutionsPaths = files.filter((filePath) => {
return solutionsRegex.test(filePath)
})
const solutions: string[] = []
for (const path of affectedSolutionsPaths) {
if (await isExistingPath(path)) {
solutions.push(path)
}
}
return solutions.map((solution) => {
const [, challengeName, , programmingLanguageName, solutionName] =
solution.split('/')
return new Solution({
challenge: new Challenge({
name: challengeName
}),
name: solutionName,
programmingLanguageName
})
})
return await Solution.getManyByPaths(affectedSolutionsPaths)
}
}

View File

@ -101,4 +101,29 @@ export class Solution implements SolutionOptions {
}
return solution
}
/**
* Get Solutions by relative paths.
* @param paths relative to `challenges` (e.g: `challenges/hello-world/solutions/c/function`)
* @returns
*/
static async getManyByPaths (paths: string[]): Promise<Solution[]> {
const solutions: string[] = []
for (const path of paths) {
if (await isExistingPath(path)) {
solutions.push(path)
}
}
return solutions.map((solution) => {
const [, challengeName, , programmingLanguageName, solutionName] =
solution.split('/')
return new Solution({
challenge: new Challenge({
name: challengeName
}),
name: solutionName,
programmingLanguageName
})
})
}
}

View File

@ -73,9 +73,7 @@ class Template {
public async solution (options: TemplateSolutionOptions): Promise<void> {
const { destination, githubUser, name, challengeName, programmingLanguageName } = options
const templateLanguagePath = path.join(TEMPLATE_SOLUTION_PATH, programmingLanguageName)
if (!(await isExistingPath(templateLanguagePath))) {
throw new Error('This programming language is not supported yet.')
}
await this.verifySupportedProgrammingLanguage(programmingLanguageName)
await fs.promises.mkdir(destination, { recursive: true })
await copyDirectory(templateLanguagePath, destination)
await copyDirectory(TEMPLATE_SOLUTION_BASE_PATH, destination)
@ -95,6 +93,13 @@ class Template {
destination
})
}
public async verifySupportedProgrammingLanguage (language: string): Promise<void> {
const templateLanguagePath = path.join(TEMPLATE_SOLUTION_PATH, language)
if (!(await isExistingPath(templateLanguagePath))) {
throw new Error('This programming language is not supported yet.')
}
}
}
export const template = new Template()

3066
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
},
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
"npm": ">=8.0.0"
},
"ts-standard": {
"ignore": [
@ -38,29 +38,29 @@
"dependencies": {
"chalk": "4.1.2",
"clipanion": "3.0.1",
"date-and-time": "2.0.0",
"date-and-time": "2.0.1",
"execa": "5.1.1",
"ora": "5.4.1",
"replace-in-file": "6.2.0",
"table": "6.7.1",
"table": "6.7.2",
"typanion": "3.5.0",
"validate-npm-package-name": "3.0.0"
},
"devDependencies": {
"@commitlint/cli": "13.1.0",
"@commitlint/config-conventional": "13.1.0",
"@commitlint/cli": "13.2.1",
"@commitlint/config-conventional": "13.2.0",
"@types/date-and-time": "0.13.0",
"@types/jest": "27.0.2",
"@types/mock-fs": "4.13.1",
"@types/node": "16.9.4",
"@types/node": "16.10.8",
"@types/validate-npm-package-name": "3.0.3",
"editorconfig-checker": "4.0.2",
"jest": "27.2.1",
"markdownlint-cli": "0.28.1",
"mock-fs": "5.1.0",
"jest": "27.2.5",
"markdownlint-cli": "0.29.0",
"mock-fs": "5.1.1",
"rimraf": "3.0.2",
"ts-jest": "27.0.5",
"ts-standard": "10.0.0",
"typescript": "4.4.3"
"typescript": "4.4.4"
}
}

View File

@ -1,4 +1,4 @@
FROM gcc:11.2.0
COPY ./ ./
RUN gcc *.c --output=solution
RUN gcc -Wall -Wextra -Werror *.c --output=solution
CMD ["./solution"]

View File

@ -1,3 +1,3 @@
FROM dart:2.14.2
FROM dart:2.14.3
COPY ./ ./
CMD ["dart", "run", "solution.dart"]

View File

@ -1,3 +1,3 @@
FROM node:16.9.1
FROM node:16.11.1
COPY ./ ./
CMD ["node", "solution.js"]

View File

@ -1,3 +1,3 @@
FROM python:3.9.7
FROM python:3.10.0
COPY ./ ./
CMD ["python", "solution.py"]

View File

@ -1,4 +1,4 @@
FROM node:16.9.1
FROM node:16.11.1
RUN npm install --global ts-node typescript @types/node
COPY ./ ./
CMD ["ts-node", "solution.ts"]

View File

@ -0,0 +1,10 @@
#include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) {
size_t length = strlen(string);
string[length] = character;
string[length + 1] = '\0';
}

View File

@ -0,0 +1,13 @@
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @brief Append a character to a string, assuming string points to an array
* with enough space.
*
* @param string
* @param character
*/
void character_append(char* string, char character);
#endif

View File

@ -0,0 +1,19 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() {
char character;
size_t length = 1;
char* string = malloc(length * sizeof(char));
*string = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string = realloc(string, length * sizeof(char));
character_append(string, character);
}
return string;
}

View File

@ -0,0 +1,11 @@
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -1,10 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "input.h"
int main() {
char input[1024];
while (scanf("%s", &input) != EOF) {
printf("Hello, %s!", input);
}
char *string = input();
printf("Hello, %s!\n", string);
return EXIT_SUCCESS;
}