Skip to content

Commit

Permalink
Remove circular dep wrecking vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
hatton committed May 4, 2024
1 parent 95ad70f commit 68a876c
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 214 deletions.
2 changes: 1 addition & 1 deletion archive-configurations/ELAR/fields.json5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
project: [
{ key: "collectionName", visibility: "always" },
// { key: "collectionName", visibility: "always" },
{ key: "collectionTitle", visibility: "always" },
{ key: "collectionDescription", visibility: "always" },
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/ui": "^1.5.0",
"@vitest/ui": "^1.6.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "10.1.0",
"babel-loader": "^8.1.0",
Expand Down Expand Up @@ -122,7 +122,7 @@
"vite-plugin-electron-renderer": "^0.12.1",
"vite-plugin-environment": "^1.1.3",
"vite-plugin-time-reporter": "^2.1.0",
"vitest": "^1.5.0",
"vitest": "^1.6.0",
"webdriverio": "^4.14.0"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/components/FieldLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { translateFieldLabel, translateTip } from "../other/localization";
import { observer } from "mobx-react";
import { tooltipBackground } from "../containers/theme";
import { Project } from "../model/Project/Project";
import { GetOtherConfigurationSettings } from "../model/Project/OtherConfigurationSettings";

export const FieldLabel: React.FunctionComponent<{
fieldDef: FieldDefinition;
Expand Down Expand Up @@ -81,7 +81,7 @@ export const FieldInfoAffordances: React.FunctionComponent<{
</CommaSeparatedIndicator>
)}

{Project.OtherConfigurationSettings.showImdiPreview &&
{GetOtherConfigurationSettings().showImdiPreview &&
props.fieldDef.omitFromImdi && <NotConsumedByArchiveIndicator />}
</div>
);
Expand Down
5 changes: 3 additions & 2 deletions src/components/FolderPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
import { useEffect } from "react";
import { ErrorBoundary } from "./ErrorBoundary";
import { useLingui } from "@lingui/react";
import { GetOtherConfigurationSettings } from "../model/Project/OtherConfigurationSettings";

export interface IProps {
folder: Folder;
Expand Down Expand Up @@ -152,7 +153,7 @@ const FileTabs: React.FunctionComponent<
/>
</TabPanel>
);
const imdiTab = Project.OtherConfigurationSettings.showImdiPreview ? (
const imdiTab = GetOtherConfigurationSettings().showImdiPreview ? (
<Tab>IMDI {/* don't translate */}</Tab>
) : (
<></>
Expand All @@ -164,7 +165,7 @@ const FileTabs: React.FunctionComponent<
<></>
);

const imdiPanel = Project.OtherConfigurationSettings.showImdiPreview ? (
const imdiPanel = GetOtherConfigurationSettings().showImdiPreview ? (
<TabPanel>
<ErrorBoundary>
<ImdiView
Expand Down
1 change: 0 additions & 1 deletion src/components/LanguageChoicesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Language, LanguageFinder } from "../languageFinder/LanguageFinder";
import _ from "lodash";
import { LanguagePill, LanguageOption } from "./LanguagePill";
import { observer } from "mobx-react";
import { c } from "vitest/dist/reporters-5f784f42.js";

const saymore_orange = "#e69664";

Expand Down
4 changes: 2 additions & 2 deletions src/components/Notes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TextFieldEdit } from "./TextFieldEdit";
import { InfoIndicator } from "./FieldIndicators";
import { css } from "@emotion/react";
import { Trans, t } from "@lingui/macro";
import { Project } from "../model/Project/Project";
import { GetOtherConfigurationSettings } from "../model/Project/OtherConfigurationSettings";

export const Notes: React.FunctionComponent<{
field: Field;
Expand All @@ -19,7 +19,7 @@ export const Notes: React.FunctionComponent<{
overflow: auto;
`}
>
{Project.OtherConfigurationSettings.showImdiPreview && (
{GetOtherConfigurationSettings().showImdiPreview && (
<InfoIndicator
css={css`
margin-left: auto;
Expand Down
10 changes: 5 additions & 5 deletions src/components/project/ProjectTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { AuthorityLists } from "../../model/Project/AuthorityLists/AuthorityList
import { ArchiveConfigurationForm } from "./ArchiveConfigurationForm";
import { ImdiView } from "../ImdiView";
import "./ProjectTab.scss";
import { ParadisecView } from "../ParadisecView";
import userSettings from "../../other/UserSettings";
import { ThemeProvider } from "@mui/material";
import { useState } from "react";
import { createProjectTheme } from "../../containers/theme";
import { ParadisecView } from "../ParadisecView";
import { LametaXmlView } from "../lametaXmlView";
import { GetOtherConfigurationSettings } from "../../model/Project/OtherConfigurationSettings";

interface IProps {
project: Project;
Expand Down Expand Up @@ -74,14 +74,14 @@ export const ProjectTab: React.FunctionComponent<IProps> = observer((props) => {
<Tab className={"tab-project-other-docs"}>
<Trans>Other Documents</Trans>
</Tab>
{Project.OtherConfigurationSettings.showImdiPreview ? (
{GetOtherConfigurationSettings().showImdiPreview ? (
<Tab className={"tab-project-imdi"}>
IMDI {/* don't translate */}
</Tab>
) : (
<></>
)}
{Project.OtherConfigurationSettings.showParadisec ? (
{GetOtherConfigurationSettings().showParadisec ? (
<Tab className={"tab-project-paradisec"}>
PARADISEC {/* don't translate */}
</Tab>
Expand Down Expand Up @@ -169,7 +169,7 @@ export const ProjectTab: React.FunctionComponent<IProps> = observer((props) => {
<br />
</FolderPane>
</TabPanel>
{Project.OtherConfigurationSettings.showImdiPreview ? (
{GetOtherConfigurationSettings().showImdiPreview ? (
<TabPanel>
<ImdiView
target={props.project}
Expand All @@ -180,7 +180,7 @@ export const ProjectTab: React.FunctionComponent<IProps> = observer((props) => {
) : (
<></>
)}
{Project.OtherConfigurationSettings.showParadisec ? (
{GetOtherConfigurationSettings().showParadisec ? (
<TabPanel>
<ParadisecView
target={props.project}
Expand Down
6 changes: 3 additions & 3 deletions src/export/ImdiBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default class ImdiBundler {
imdiMode === IMDIMode.OPEX
? Path.basename(session.directory)
: "", // with opex, the metadata file goes into the folder it describes. Else, on the level above.
sanitizeForArchive(imdiFileName, true)
sanitizeForArchive(imdiFileName, "ASCII")
),
sessionImdi
);
Expand Down Expand Up @@ -234,7 +234,7 @@ export default class ImdiBundler {
// that renames that change the link name are used.
sanitizeForArchive(
f.getNameToUseWhenExportingUsingTheActualFile(),
true
"ASCII"
)
),
(progressMessage) => {}
Expand Down Expand Up @@ -327,7 +327,7 @@ export default class ImdiBundler {
fs.writeFileSync(
Path.join(
directoryForMetadataXmlFile,
sanitizeForArchive(imdiFileName, true)
sanitizeForArchive(imdiFileName, "ASCII")
),
imdiXml
);
Expand Down
File renamed without changes.
67 changes: 67 additions & 0 deletions src/export/ImdiGenerator-corpus-metadata.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import ImdiGenerator, { IMDIMode } from "./ImdiGenerator";
import { Project } from "../model/Project/Project";
import {
setResultXml,
xexpect as expect,
count,
value,
xexpect
} from "../other/xmlUnitTestUtils";
import temp from "temp";
import * as fs from "fs-extra";
import assert from "assert";
import {
describe,
it,
vi,
beforeAll,
afterAll,
test,
afterEach,
beforeEach
} from "vitest";
import { GetProjectFileWithOneField } from "../model/Project/WriteProject.spec";

temp.track(); // cleanup on exit: doesn't work

let project: Project;
let projectDir: string;

describe("Imdi generation for images", () => {
beforeEach(() => {
projectDir = temp.mkdirSync("lameta imdi actor generator test");
project = Project.fromDirectory(projectDir);
});

afterEach(() => {
fs.emptyDirSync(projectDir);
fs.removeSync(projectDir);
});

test("fundingProjectTitle goes to the right place", () => {
// NB: in ELAR, at least, this will appear as "Collection Title"
project.properties.setText("fundingProjectTitle", "my title");
project.properties.setText("projectDescription", "my description");
project.properties.setText("collectionSteward", "my steward");
const x = ImdiGenerator.generateCorpus(
IMDIMode.RAW_IMDI,
project,
[],
true
);

setResultXml(x);

// From Hanna: Collection Title = Corpus/Title
// From Hanna: Collection Key = Corpus/MDGroup/Keys/Key[@Name='CorpusId']
// The ELAR fields.json renames the label for fundingProjectTitle to "Collection Title"
expect("//Corpus/Title").toMatch("my title");

// From Hanna: Collection Description= Corpus/Description[@Name='short_description']
// The ELAR fields.json renames the label for projectDescription to "Collection Description"
expect('//Corpus/Description[@Name="short_description"]').toMatch(
"my description"
);
// From Hanna: Collection Steward = Corpus/MDGroup/Actors/Actor[@Role='Collection Steward']
});
});
4 changes: 2 additions & 2 deletions src/export/ImdiGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default class ImdiGenerator {
}
return this.makeString();
}
private projectXmlForPreview(): string {
public projectXmlForPreview(): string {
this.tail = XmlBuilder.create("Project");
this.addProjectInfo();
return this.makeString();
Expand Down Expand Up @@ -595,7 +595,7 @@ export default class ImdiGenerator {
// files as they get copied to the export, regardless of that setting. This is because this is a *requirement* of
// IMDI archives. Anyhow, since the bundler would have (or will have) export the sanitized version, we need to do
// that to the file name we use for it in the xml.
const filename = sanitizeForArchive(Path.basename(path), true);
const filename = sanitizeForArchive(Path.basename(path), "ASCII");
const immediateParentDirectoryName = Path.basename(Path.dirname(path));

const relativePath = Path.join(immediateParentDirectoryName, filename)
Expand Down
30 changes: 30 additions & 0 deletions src/model/Project/OtherConfigurationSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export type OtherConfigurationSettings = {
configurationFullName: string;
showImdiPreview: boolean;
showParadisec: boolean;
fileNameRules: "ASCII" | "unicode";
};

let otherConfigurationSettings: OtherConfigurationSettings = {
configurationFullName: "",
showImdiPreview: false,
showParadisec: false,
fileNameRules: "ASCII"
};

export function SetOtherConfigurationSettings(
settings: OtherConfigurationSettings
) {
otherConfigurationSettings = settings;
}
export function GetOtherConfigurationSettings(): OtherConfigurationSettings {
return otherConfigurationSettings;
}
export function resetOtherConfigurationSettings() {
otherConfigurationSettings = {
showImdiPreview: false,
showParadisec: false,
configurationFullName: "",
fileNameRules: "ASCII"
};
}
1 change: 0 additions & 1 deletion src/model/Project/Person/Person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from "./PersonMigration";
import xmlbuilder from "xmlbuilder";
import { makeObservable, observable } from "mobx";
import { Project } from "../Project";

export type idChangeHandler = (oldId: string, newId: string) => void;
export const maxOtherLanguages = 10;
Expand Down
39 changes: 10 additions & 29 deletions src/model/Project/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import { setCurrentProjectId } from "./MediaFolderAccess";
import { CapitalCase } from "../../other/case";
import { IChoice } from "../field/Field";
import { locateDependencyForFilesystemCall } from "../../other/locateDependency";
import {
GetOtherConfigurationSettings,
SetOtherConfigurationSettings
} from "./OtherConfigurationSettings";

let sCurrentProject: Project | null = null;

Expand Down Expand Up @@ -72,12 +76,7 @@ export class ProjectHolder {
sCurrentProject = p;
}
}
type OtherConfigurationSettings = {
configurationFullName: string;
showImdiPreview: boolean;
showParadisec: boolean;
fileNameRules: "ASCII" | "unicode";
};

export class Project extends Folder {
public loadingError: string;

Expand All @@ -93,28 +92,10 @@ export class Project extends Folder {
public authorityLists: AuthorityLists;
public languageFinder: LanguageFinder;

public otherConfigurationSettings: OtherConfigurationSettings = {
configurationFullName: "",
showImdiPreview: false,
showParadisec: false,
fileNameRules: "ASCII"
};

public get folderType(): IFolderType {
return "project";
}

public static get OtherConfigurationSettings(): OtherConfigurationSettings {
return sCurrentProject === null
? {
showImdiPreview: false,
showParadisec: false,
configurationFullName: "",
fileNameRules: "ASCII"
}
: sCurrentProject.otherConfigurationSettings;
}

public static getDefaultContentLanguageCode() {
const codeAndName =
sCurrentProject === null
Expand Down Expand Up @@ -962,8 +943,8 @@ export class Project extends Folder {
const factoryPath = locateDependencyForFilesystemCall(
`archive-configurations/lameta/settings.json5`
);
this.otherConfigurationSettings = JSON5.parse(
fs.readFileSync(factoryPath, "utf8")
SetOtherConfigurationSettings(
JSON5.parse(fs.readFileSync(factoryPath, "utf8"))
);

// now see if there are any settings in the confuration that
Expand All @@ -988,10 +969,10 @@ export class Project extends Folder {
}
// read in these settings and merge them with the defaults
const settings = JSON5.parse(fs.readFileSync(path, "utf8"));
this.otherConfigurationSettings = {
...this.otherConfigurationSettings,
SetOtherConfigurationSettings({
...GetOtherConfigurationSettings(),
...settings
};
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/Project/WriteProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function AttemptRoundTripOfOneField(
}
}

function GetProjectFileWithOneField(
export function GetProjectFileWithOneField(
tag: string,
content: string
): ProjectMetadataFile {
Expand Down
4 changes: 2 additions & 2 deletions src/other/sanitizeForArchive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { describe, it, expect } from "vitest";

describe("SanitizeForArchive", () => {
it("in IMDI mode, it replaces ! with underscores", () => {
const name = sanitizeForArchive("foo!bar!bas", true);
const name = sanitizeForArchive("foo!bar!bas", "ASCII");
expect(name).toBe("foo_bar_bas");
});
it("in IMDI mode, it doesn't leave trailing underscores", () => {
const name = sanitizeForArchive("foo!", true);
const name = sanitizeForArchive("foo!", "ASCII");
expect(name).toBe("foo");
});
});
Loading

0 comments on commit 68a876c

Please sign in to comment.