Skip to content

Commit

Permalink
test(cli): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaslz committed Jan 28, 2024
1 parent 8764b04 commit 2b1cb08
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion packages/cli/src/utils/hosts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import consola from "consola";
import fs from "fs";

import { HOSTS_END, HOSTS_START } from "@/constants";
import {
getContentFromHosts,
setContentToHosts,
updateHosts,
} from "@/utils/hosts";

import { getContentFromHosts, setContentToHosts, updateHosts } from "./hosts";
vi.mock("@/utils/update-system-hosts");

describe("Utils - getContentFromHosts, setContentToHosts, updateHosts", () => {
test("move domain if already exists without Local SSL config slot", async () => {
Expand All @@ -14,6 +19,15 @@ describe("Utils - getContentFromHosts, setContentToHosts, updateHosts", () => {

const result = await getContentFromHosts();

// read files
expect(fs.readFileSync).toBeCalledTimes(1);
expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", {
encoding: "utf8",
});

// write files
expect(fs.writeFileSync).not.toBeCalled();

expect(result).toBe("");
});

Expand All @@ -25,6 +39,15 @@ describe("Utils - getContentFromHosts, setContentToHosts, updateHosts", () => {

const result = await getContentFromHosts();

// read files
expect(fs.readFileSync).toBeCalledTimes(1);
expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", {
encoding: "utf8",
});

// write files
expect(fs.writeFileSync).not.toBeCalled();

expect(result).toBe("");
});

Expand All @@ -42,6 +65,15 @@ ${HOSTS_END}

const result = await getContentFromHosts();

// read files
expect(fs.readFileSync).toBeCalledTimes(1);
expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", {
encoding: "utf8",
});

// write files
expect(fs.writeFileSync).not.toBeCalled();

expect(result).toBe(
`
127.0.0.1 local.domain.com
Expand Down Expand Up @@ -106,6 +138,13 @@ ${HOSTS_END}
`.trim(),
);

// read files
expect(fs.readFileSync).toBeCalledTimes(1);
expect(fs.readFileSync).nthCalledWith(1, "/etc/hosts", {
encoding: "utf8",
});

// write files
expect(fs.writeFileSync).toBeCalledTimes(1);
expect(fs.writeFileSync).toBeCalledWith(
expect.stringMatching(/\/root\/path\/.tmp-hosts/i),
Expand Down

0 comments on commit 2b1cb08

Please sign in to comment.