11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This test checks that the changelogs contain an entry for releases.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst fs = require('fs');
81cb0ef41Sopenharmony_ciconst path = require('path');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst getDefine = (text, name) => {
111cb0ef41Sopenharmony_ci  const regexp = new RegExp(`#define\\s+${name}\\s+(.*)`);
121cb0ef41Sopenharmony_ci  const match = regexp.exec(text);
131cb0ef41Sopenharmony_ci  assert.notStrictEqual(match, null);
141cb0ef41Sopenharmony_ci  return match[1];
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst srcRoot = path.join(__dirname, '..', '..');
181cb0ef41Sopenharmony_ciconst mainChangelogFile = path.join(srcRoot, 'CHANGELOG.md');
191cb0ef41Sopenharmony_ciconst versionFile = path.join(srcRoot, 'src', 'node_version.h');
201cb0ef41Sopenharmony_ciconst versionText = fs.readFileSync(versionFile, { encoding: 'utf8' });
211cb0ef41Sopenharmony_ciconst release = getDefine(versionText, 'NODE_VERSION_IS_RELEASE') !== '0';
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciif (!release) {
241cb0ef41Sopenharmony_ci  common.skip('release bit is not set');
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst major = getDefine(versionText, 'NODE_MAJOR_VERSION');
281cb0ef41Sopenharmony_ciconst minor = getDefine(versionText, 'NODE_MINOR_VERSION');
291cb0ef41Sopenharmony_ciconst patch = getDefine(versionText, 'NODE_PATCH_VERSION');
301cb0ef41Sopenharmony_ciconst versionForRegex = `${major}\\.${minor}\\.${patch}`;
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst lts = getDefine(versionText, 'NODE_VERSION_IS_LTS') !== '0';
331cb0ef41Sopenharmony_ciconst codename = getDefine(versionText, 'NODE_VERSION_LTS_CODENAME').slice(1, -1);
341cb0ef41Sopenharmony_ci// If the LTS bit is set there should be a codename.
351cb0ef41Sopenharmony_ciif (lts) {
361cb0ef41Sopenharmony_ci  assert.notStrictEqual(codename, '');
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
401cb0ef41Sopenharmony_ci// Check CHANGELOG_V*.md
411cb0ef41Sopenharmony_ci{
421cb0ef41Sopenharmony_ci  const changelog = fs.readFileSync(path.join(srcRoot, changelogPath), { encoding: 'utf8' });
431cb0ef41Sopenharmony_ci  // Check title matches major version.
441cb0ef41Sopenharmony_ci  assert.match(changelog, new RegExp(`# Node\\.js ${major} ChangeLog`));
451cb0ef41Sopenharmony_ci  // Check table header
461cb0ef41Sopenharmony_ci  let tableHeader;
471cb0ef41Sopenharmony_ci  if (lts) {
481cb0ef41Sopenharmony_ci    tableHeader = new RegExp(`<th>LTS '${codename}'</th>`);
491cb0ef41Sopenharmony_ci  } else {
501cb0ef41Sopenharmony_ci    tableHeader = /<th>Current<\/th>/;
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci  assert.match(changelog, tableHeader);
531cb0ef41Sopenharmony_ci  // Check table contains link to this release.
541cb0ef41Sopenharmony_ci  assert.match(changelog, new RegExp(`<a href="#${versionForRegex}">${versionForRegex}</a>`));
551cb0ef41Sopenharmony_ci  // Check anchor for this release.
561cb0ef41Sopenharmony_ci  assert.match(changelog, new RegExp(`<a id="${versionForRegex}"></a>`));
571cb0ef41Sopenharmony_ci  // Check title for changelog entry.
581cb0ef41Sopenharmony_ci  let title;
591cb0ef41Sopenharmony_ci  if (lts) {
601cb0ef41Sopenharmony_ci    title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${codename}' \\(LTS\\), @\\S+`);
611cb0ef41Sopenharmony_ci  } else {
621cb0ef41Sopenharmony_ci    title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} \\(Current\\), @\\S+`);
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci  assert.match(changelog, title);
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// Main CHANGELOG.md checks
681cb0ef41Sopenharmony_ci{
691cb0ef41Sopenharmony_ci  const mainChangelog = fs.readFileSync(mainChangelogFile, { encoding: 'utf8' });
701cb0ef41Sopenharmony_ci  // Check for the link to the appropriate CHANGELOG_V*.md file.
711cb0ef41Sopenharmony_ci  let linkToChangelog;
721cb0ef41Sopenharmony_ci  if (lts) {
731cb0ef41Sopenharmony_ci    linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Long Term Support\\*\\*`);
741cb0ef41Sopenharmony_ci  } else {
751cb0ef41Sopenharmony_ci    linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Current\\*\\*`);
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci  assert.match(mainChangelog, linkToChangelog);
781cb0ef41Sopenharmony_ci  // Check table header.
791cb0ef41Sopenharmony_ci  let tableHeader;
801cb0ef41Sopenharmony_ci  if (lts) {
811cb0ef41Sopenharmony_ci    tableHeader = new RegExp(`<th title="LTS Until \\d{4}-\\d{2}"><a href="${changelogPath}">${major}</a> \\(LTS\\)</th>`);
821cb0ef41Sopenharmony_ci  } else {
831cb0ef41Sopenharmony_ci    tableHeader = new RegExp(`<th title="Current"><a href="${changelogPath}">${major}</a> \\(Current\\)</th>`);
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci  assert.match(mainChangelog, tableHeader);
861cb0ef41Sopenharmony_ci  // Check the table contains a link to the release in the appropriate CHANGELOG_V*.md file.
871cb0ef41Sopenharmony_ci  const linkToVersion = new RegExp(`<b><a href="${changelogPath}#${versionForRegex}">${versionForRegex}</a></b><br/>`);
881cb0ef41Sopenharmony_ci  assert.match(mainChangelog, linkToVersion);
891cb0ef41Sopenharmony_ci}
90