Skip to content

Commit

Permalink
feat: extract codecov uploading to a blockCodecov (#1865)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1848
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

Also removes `addons.flags` from `blockVitest` since this repo won't use
them after #1839.

💖
  • Loading branch information
JoshuaKGoldberg authored Jan 15, 2025
1 parent 66db26e commit 444de1c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 90 deletions.
129 changes: 129 additions & 0 deletions src/next/blocks/blockCodecov.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { testBlock } from "create-testers";
import { describe, expect, test } from "vitest";

import { blockCodecov } from "./blockCodecov.js";
import { optionsBase } from "./options.fakes.js";

describe("blockCodecov", () => {
test("without addons or mode", () => {
const creation = testBlock(blockCodecov, {
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"apps": [
{
"name": "Codecov",
"url": "https://github.com/apps/codecov",
},
],
},
"block": [Function],
},
{
"addons": {
"actionSteps": [
{
"if": "always()",
"uses": "codecov/codecov-action@v3",
},
],
},
"block": [Function],
},
],
}
`);
});

test("migration mode", () => {
const creation = testBlock(blockCodecov, {
mode: "migrate",
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"apps": [
{
"name": "Codecov",
"url": "https://github.com/apps/codecov",
},
],
},
"block": [Function],
},
{
"addons": {
"actionSteps": [
{
"if": "always()",
"uses": "codecov/codecov-action@v3",
},
],
},
"block": [Function],
},
],
"scripts": [
{
"commands": [
"rm .github/codecov.yml codecov.yml",
],
"phase": 0,
},
],
}
`);
});

test("with addons", () => {
const creation = testBlock(blockCodecov, {
addons: {
env: {
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}",
},
},
options: optionsBase,
});

expect(creation).toMatchInlineSnapshot(`
{
"addons": [
{
"addons": {
"apps": [
{
"name": "Codecov",
"url": "https://github.com/apps/codecov",
},
],
},
"block": [Function],
},
{
"addons": {
"actionSteps": [
{
"env": {
"CODECOV_TOKEN": "\${{ secrets.CODECOV_TOKEN }}",
},
"if": "always()",
"uses": "codecov/codecov-action@v3",
},
],
},
"block": [Function],
},
],
}
`);
});
});
47 changes: 47 additions & 0 deletions src/next/blocks/blockCodecov.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { z } from "zod";

import { base } from "../base.js";
import { blockGitHubApps } from "./blockGitHubApps.js";
import { blockVitest } from "./blockVitest.js";
import { CommandPhase } from "./phases.js";

export const blockCodecov = base.createBlock({
about: {
name: "Codecov",
},
addons: { env: z.record(z.string(), z.string()).optional() },
migrate() {
return {
scripts: [
{
commands: ["rm .github/codecov.yml codecov.yml"],
phase: CommandPhase.Migrations,
},
],
};
},
produce({ addons }) {
const { env } = addons;
return {
addons: [
blockGitHubApps({
apps: [
{
name: "Codecov",
url: "https://github.com/apps/codecov",
},
],
}),
blockVitest({
actionSteps: [
{
...(env && { env }),
if: "always()",
uses: "codecov/codecov-action@v3",
},
],
}),
],
};
},
});
23 changes: 10 additions & 13 deletions src/next/blocks/blockGitHubActionsCI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import { createSoloWorkflowFile } from "../../steps/writing/creation/dotGitHub/c
import { base } from "../base.js";
import { CommandPhase } from "./phases.js";

export const zActionStep = z.intersection(
z.object({
env: z.record(z.string(), z.string()).optional(),
if: z.string().optional(),
with: z.record(z.string(), z.string()).optional(),
}),
z.union([z.object({ run: z.string() }), z.object({ uses: z.string() })]),
);

export const blockGitHubActionsCI = base.createBlock({
about: {
name: "GitHub Actions CI",
Expand All @@ -15,19 +24,7 @@ export const blockGitHubActionsCI = base.createBlock({
.array(
z.object({
name: z.string(),
steps: z.array(
z.intersection(
z.object({
env: z.record(z.string(), z.string()).optional(),
if: z.string().optional(),
with: z.record(z.string(), z.string()).optional(),
}),
z.union([
z.object({ run: z.string() }),
z.object({ uses: z.string() }),
]),
),
),
steps: z.array(zActionStep),
}),
)
.optional(),
Expand Down
51 changes: 1 addition & 50 deletions src/next/blocks/blockVitest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,12 @@ describe("blockVitest", () => {
{
"run": "pnpm run test --coverage",
},
{
"if": "always()",
"uses": "codecov/codecov-action@v3",
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"apps": [
{
"name": "Codecov",
"url": "https://github.com/apps/codecov",
},
],
},
"block": [Function],
},
{
"addons": {
"properties": {
Expand Down Expand Up @@ -398,27 +383,12 @@ describe("blockVitest", () => {
{
"run": "pnpm run test --coverage",
},
{
"if": "always()",
"uses": "codecov/codecov-action@v3",
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"apps": [
{
"name": "Codecov",
"url": "https://github.com/apps/codecov",
},
],
},
"block": [Function],
},
{
"addons": {
"properties": {
Expand Down Expand Up @@ -499,7 +469,7 @@ describe("blockVitest", () => {
"scripts": [
{
"commands": [
"rm .github/codecov.yml .mocha* codecov.yml jest.config.* vitest.config.*",
"rm .mocha* jest.config.* vitest.config.*",
],
"phase": 0,
},
Expand All @@ -514,7 +484,6 @@ describe("blockVitest", () => {
coverage: {
directory: "coverage*",
exclude: ["other"],
flags: "unit",
include: ["src/"],
},
exclude: ["lib/"],
Expand Down Expand Up @@ -662,30 +631,12 @@ describe("blockVitest", () => {
{
"run": "pnpm run test --coverage",
},
{
"if": "always()",
"uses": "codecov/codecov-action@v3",
"with": {
"flags": "unit",
},
},
],
},
],
},
"block": [Function],
},
{
"addons": {
"apps": [
{
"name": "Codecov",
"url": "https://github.com/apps/codecov",
},
],
},
"block": [Function],
},
{
"addons": {
"properties": {
Expand Down
33 changes: 6 additions & 27 deletions src/next/blocks/blockVitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { blockCSpell } from "./blockCSpell.js";
import { blockDevelopmentDocs } from "./blockDevelopmentDocs.js";
import { blockESLint } from "./blockESLint.js";
import { blockExampleFiles } from "./blockExampleFiles.js";
import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
import { blockGitHubApps } from "./blockGitHubApps.js";
import { blockGitHubActionsCI, zActionStep } from "./blockGitHubActionsCI.js";
import { blockGitignore } from "./blockGitignore.js";
import { blockPackageJson } from "./blockPackageJson.js";
import { blockPrettier } from "./blockPrettier.js";
Expand All @@ -20,32 +19,28 @@ export const blockVitest = base.createBlock({
name: "Vitest",
},
addons: {
actionSteps: z.array(zActionStep).default([]),
coverage: z
.object({
directory: z.string().optional(),
exclude: z.array(z.string()).optional(),
flags: z.string().optional(),
include: z.array(z.string()).optional(),
})
.default({}),
env: z.record(z.string(), z.string()).default({}),
exclude: z.array(z.string()).default([]),
flags: z.array(z.string()).default([]),
},
migrate() {
return {
scripts: [
{
commands: [
"rm .github/codecov.yml .mocha* codecov.yml jest.config.* vitest.config.*",
],
commands: ["rm .mocha* jest.config.* vitest.config.*"],
phase: CommandPhase.Migrations,
},
],
};
},
produce({ addons }) {
const { coverage, env, exclude = [], flags } = addons;
const { actionSteps, coverage, exclude = [] } = addons;
const coverageDirectory = coverage.directory ?? "coverage";
const excludeText = JSON.stringify(exclude);

Expand Down Expand Up @@ -152,23 +147,7 @@ describe("greet", () => {
jobs: [
{
name: "Test",
steps: [
{ run: "pnpm run test --coverage" },
{
...(Object.keys(env).length && { env }),
if: "always()",
uses: "codecov/codecov-action@v3",
...(coverage.flags && { with: { flags: coverage.flags } }),
},
],
},
],
}),
blockGitHubApps({
apps: [
{
name: "Codecov",
url: "https://github.com/apps/codecov",
steps: [{ run: "pnpm run test --coverage" }, ...actionSteps],
},
],
}),
Expand All @@ -181,7 +160,7 @@ describe("greet", () => {
"vitest",
),
scripts: {
test: `vitest ${flags.join(" ")}`.trim(),
test: "vitest",
},
},
}),
Expand Down
Loading

0 comments on commit 444de1c

Please sign in to comment.