Skip to content

Commit 6a11d5a

Browse files
CopilotApollon77
andauthored
Fix GitHub Actions workflow to respect minimum Node.js version selection and centralize version fallbacks (#1161)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Apollon77 <11976694+Apollon77@users.noreply.github.com> Co-authored-by: Ingo Fischer <github@fischer-ka.de>
1 parent 92f4d9d commit 6a11d5a

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

src/lib/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Constants for default and fallback values used throughout the adapter creation process
3+
*/
4+
5+
/** The recommended Node.js version to use as a fallback when none is specified */
6+
export const RECOMMENDED_NODE_VERSION_FALLBACK = "20";

templates/_github/workflows/test-and-release.yml.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TemplateFunction } from "../../../src/lib/createAdapter";
2+
import { RECOMMENDED_NODE_VERSION_FALLBACK } from "../../../src/lib/constants";
23

34
const templateFunction: TemplateFunction = answers => {
45

@@ -14,8 +15,17 @@ const templateFunction: TemplateFunction = answers => {
1415
const useReleaseScript = answers.releaseScript === "yes";
1516
const isGitHub = answers.target === "github";
1617

17-
const ltsNodeVersion = "20.x";
18-
const adapterTestVersions = ["20.x", "22.x", "24.x"];
18+
// Determine the LTS version and test versions based on the minimum Node.js version selected
19+
const minNodeVersion = answers.nodeVersion || RECOMMENDED_NODE_VERSION_FALLBACK;
20+
const ltsNodeVersion = `${minNodeVersion}.x`;
21+
22+
// Filter test versions to only include versions >= the minimum version
23+
const allTestVersions = ["20.x", "22.x", "24.x"];
24+
const minVersionNumber = parseInt(minNodeVersion, 10);
25+
const adapterTestVersions = allTestVersions.filter(version => {
26+
const versionNumber = parseInt(version.split('.')[0], 10);
27+
return versionNumber >= minVersionNumber;
28+
});
1929

2030
const adapterTestOS = ["ubuntu-latest", "windows-latest", "macos-latest"];
2131

templates/package.json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { licenses } from "../src/lib/core/licenses";
33
import { getDefaultAnswer } from "../src/lib/core/questions";
44
import type { TemplateFunction } from "../src/lib/createAdapter";
55
import { fetchPackageReferenceVersion, getPackageName } from "../src/lib/packageVersions";
6+
import { RECOMMENDED_NODE_VERSION_FALLBACK } from "../src/lib/constants";
67

78
const templateFunction: TemplateFunction = async answers => {
89

@@ -23,7 +24,7 @@ const templateFunction: TemplateFunction = async answers => {
2324
const useNyc = answers.tools && answers.tools.indexOf("code coverage") > -1;
2425
const useReleaseScript = answers.releaseScript === "yes";
2526

26-
const minNodeVersion = answers.nodeVersion ?? "20";
27+
const minNodeVersion = answers.nodeVersion ?? RECOMMENDED_NODE_VERSION_FALLBACK;
2728

2829
const dependencyPromises = [
2930
...(isAdapter ? ["@iobroker/adapter-core"] : [])

templates/tsconfig.json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { TemplateFunction } from "../src/lib/createAdapter";
2+
import { RECOMMENDED_NODE_VERSION_FALLBACK } from "../src/lib/constants";
23

34
export = (answers => {
45

56
const useTypeScript = answers.language === "TypeScript";
67
const useTypeChecking = answers.tools && answers.tools.indexOf("type checking") > -1;
78
if (!useTypeScript && !useTypeChecking) return;
89

9-
const minNodeVersion = answers.nodeVersion ?? "20";
10+
const minNodeVersion = answers.nodeVersion ?? RECOMMENDED_NODE_VERSION_FALLBACK;
1011

1112
let include: string;
1213
let exclude: string;

test/create-adapter.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,5 +673,85 @@ describe("adapter creation =>", () => {
673673
);
674674
});
675675
});
676+
677+
describe("GitHub Actions workflow Node.js version filtering", () => {
678+
it("should only test supported Node.js versions", async () => {
679+
const testCases = [
680+
{
681+
nodeVersion: "20",
682+
expectedVersions: ["20.x", "22.x", "24.x"],
683+
expectedLts: "20.x",
684+
},
685+
{
686+
nodeVersion: "22",
687+
expectedVersions: ["22.x", "24.x"],
688+
expectedLts: "22.x",
689+
},
690+
{
691+
nodeVersion: "24",
692+
expectedVersions: ["24.x"],
693+
expectedLts: "24.x",
694+
},
695+
];
696+
697+
for (const testCase of testCases) {
698+
const answers: Answers = {
699+
...baseAnswers,
700+
nodeVersion: testCase.nodeVersion as "20" | "22" | "24",
701+
target: "github",
702+
releaseScript: "yes",
703+
};
704+
705+
const files = await createAdapter(answers);
706+
const workflowFile = files.find((f) =>
707+
f.name.endsWith("test-and-release.yml"),
708+
);
709+
710+
if (!workflowFile) {
711+
throw new Error(
712+
`Workflow file not found for Node.js ${testCase.nodeVersion}`,
713+
);
714+
}
715+
716+
const content = workflowFile.content;
717+
718+
// Check that the matrix includes only the expected versions
719+
const matrixMatch = content.match(
720+
/node-version: \[(.*?)\]/,
721+
);
722+
if (!matrixMatch) {
723+
throw new Error(
724+
`Matrix node versions not found for Node.js ${testCase.nodeVersion}`,
725+
);
726+
}
727+
728+
const actualVersions = matrixMatch[1]
729+
.split(", ")
730+
.map((v) => v.trim());
731+
actualVersions.should.deep.equal(
732+
testCase.expectedVersions,
733+
`For Node.js ${testCase.nodeVersion}, expected versions ${testCase.expectedVersions.join(", ")} but got ${actualVersions.join(", ")}`,
734+
);
735+
736+
// Check that the LTS version is correct
737+
const ltsMatches =
738+
content.match(/node-version: '(\d+\.x)'/g) || [];
739+
if (ltsMatches.length === 0) {
740+
throw new Error(
741+
`LTS node version not found for Node.js ${testCase.nodeVersion}`,
742+
);
743+
}
744+
745+
// All LTS references should use the expected version
746+
for (const ltsMatch of ltsMatches) {
747+
const ltsVersion = ltsMatch.match(/'(\d+\.x)'/)?.[1];
748+
ltsVersion.should.equal(
749+
testCase.expectedLts,
750+
`For Node.js ${testCase.nodeVersion}, expected LTS ${testCase.expectedLts} but got ${ltsVersion}`,
751+
);
752+
}
753+
}
754+
});
755+
});
676756
});
677757
});

0 commit comments

Comments
 (0)