diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index e3e6c484..90d3b3be 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -18,6 +18,8 @@ jobs: steps: - name: Checkout this repository uses: actions/checkout@v2 + with: + lfs: true - name: Checkout actions repository uses: actions/checkout@v2 @@ -40,7 +42,8 @@ jobs: steps: - name: Checkout this repository uses: actions/checkout@v2 - + with: + lfs: true - name: Checkout actions repository uses: actions/checkout@v2 with: diff --git a/src/parsers/parsers.js b/src/parsers/parsers.js index 2e9924c3..eaa164dc 100644 --- a/src/parsers/parsers.js +++ b/src/parsers/parsers.js @@ -1,11 +1,33 @@ import cif from "./cif"; import espresso from "./espresso"; import poscar from "./poscar"; +import { getLineFromContent } from "./utils"; import xyz from "./xyz"; +/** + * Function returns the of the file that should contain file contents based on the fileExtension value + * @param {String} fileContent + * @param {String} fileExtension + * @param {String} failoverName + * @returns {String} + */ +export function getNameFromContents(fileContent, fileExtension, failoverName = "material") { + let lineNumber; + if (fileExtension === "poscar") { + lineNumber = 0; + } + if (fileExtension === "xyz") { + lineNumber = 1; + } + const nameFromContent = getLineFromContent(fileContent, lineNumber); + if (nameFromContent) return nameFromContent; + return failoverName; +} + export default { xyz, poscar, cif, espresso, + getNameFromContents, }; diff --git a/src/parsers/utils.js b/src/parsers/utils.js new file mode 100644 index 00000000..7103121e --- /dev/null +++ b/src/parsers/utils.js @@ -0,0 +1,4 @@ +export function getLineFromContent(fileContent, lineNumber) { + const lineString = fileContent.split(/\r?\n/)[lineNumber]; + return lineString.trim(); +} diff --git a/tests/enums.js b/tests/enums.js index 1b876f27..8e0517f5 100644 --- a/tests/enums.js +++ b/tests/enums.js @@ -31,3 +31,5 @@ export const SiPWSCFInput = readFile(path.join(FIXTURES_DIR, "Si-pwscf.in")); export const Zr1H23Zr1H1 = readJSONFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.json")); export const Zr1H23Zr1H1Poscar = readFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.poscar")); export const H2O = readFile(path.join(FIXTURES_DIR, "H2O.poscar")); +export const GenericPoscar = readFile(path.join(FIXTURES_DIR, "POSCAR.poscar")); +export const GenericXYZ = readFile(path.join(FIXTURES_DIR, "Structure.xyz")); diff --git a/tests/fixtures/POSCAR.poscar b/tests/fixtures/POSCAR.poscar new file mode 100644 index 00000000..30e9f354 --- /dev/null +++ b/tests/fixtures/POSCAR.poscar @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:937fe0ff44defdce92042e6261e79bc2d425c87b33d2b412f20883e39384833f +size 302 diff --git a/tests/fixtures/Structure.xyz b/tests/fixtures/Structure.xyz new file mode 100644 index 00000000..90afc92c --- /dev/null +++ b/tests/fixtures/Structure.xyz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:476366d715b7f4149bbfc56bd65a65bd2bcdfde9a2bc6b546322b6ad89204855 +size 195 diff --git a/tests/parsers/poscar.js b/tests/parsers/poscar.js index c4642fb1..47c29ad8 100644 --- a/tests/parsers/poscar.js +++ b/tests/parsers/poscar.js @@ -1,8 +1,9 @@ import { expect } from "chai"; import { Material } from "../../src/material"; +import { getNameFromContents } from "../../src/parsers/parsers"; import { atomsCount } from "../../src/parsers/poscar"; -import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; +import { GenericPoscar, H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums"; describe("Parsers.POSCAR", () => { it("should return a valid poscar", () => { @@ -18,4 +19,7 @@ describe("Parsers.POSCAR", () => { it("should return the number of atoms for a molecule in a poscar file", () => { expect(atomsCount(H2O)).to.be.equal(3); }); + it("should return the material name based on the poscar file contents", () => { + expect(getNameFromContents(GenericPoscar, "poscar")).to.be.equal("H2S"); + }); }); diff --git a/tests/parsers/xyz.js b/tests/parsers/xyz.js index 6d685627..b63f614c 100644 --- a/tests/parsers/xyz.js +++ b/tests/parsers/xyz.js @@ -1,5 +1,7 @@ -import parsers from "../../src/parsers/parsers"; -import { Si } from "../enums"; +import { expect } from "chai"; + +import parsers, { getNameFromContents } from "../../src/parsers/parsers"; +import { GenericXYZ, Si } from "../enums"; import { assertDeepAlmostEqual } from "../utils"; describe("Parsers:XYZ", () => { @@ -11,4 +13,7 @@ describe("Parsers:XYZ", () => { "units", ]); }); + it("should return the material name based on the xyz file contents", () => { + expect(getNameFromContents(GenericXYZ, "xyz")).to.be.equal("Methane"); + }); });