|
| 1 | +import { beforeEach, describe, expect, it, jest } from "@jest/globals"; |
| 2 | + |
| 3 | +const info = jest.fn(); |
| 4 | +const warning = jest.fn(); |
| 5 | + |
| 6 | +jest.unstable_mockModule("@actions/core", () => ({ |
| 7 | + debug: jest.fn(), |
| 8 | + info, |
| 9 | + warning, |
| 10 | +})); |
| 11 | + |
| 12 | +const { findRuffVersionInSpec, getRuffVersionFromFile } = await import( |
| 13 | + "../../src/version/file-parser" |
| 14 | +); |
| 15 | + |
| 16 | +describe("file-parser", () => { |
| 17 | + beforeEach(() => { |
| 18 | + info.mockReset(); |
| 19 | + warning.mockReset(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("findRuffVersionInSpec", () => { |
| 23 | + it("extracts version from 'ruff==0.9.3'", () => { |
| 24 | + const result = findRuffVersionInSpec("ruff==0.9.3"); |
| 25 | + expect(result).toBe("0.9.3"); |
| 26 | + expect(info).toHaveBeenCalledWith( |
| 27 | + "Found ruff version in requirements file: 0.9.3", |
| 28 | + ); |
| 29 | + expect(warning).not.toHaveBeenCalled(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("extracts version from 'ruff>=0.14'", () => { |
| 33 | + const result = findRuffVersionInSpec("ruff>=0.14"); |
| 34 | + expect(result).toBe(">=0.14"); |
| 35 | + expect(warning).not.toHaveBeenCalled(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("extracts version from 'ruff ~=1.0.0'", () => { |
| 39 | + const result = findRuffVersionInSpec("ruff ~=1.0.0"); |
| 40 | + expect(result).toBe("~=1.0.0"); |
| 41 | + expect(warning).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("extracts version from 'ruff>=0.14,<1.0'", () => { |
| 45 | + const result = findRuffVersionInSpec("ruff>=0.14,<1.0"); |
| 46 | + expect(result).toBe(">=0.14,<1.0"); |
| 47 | + expect(warning).not.toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("extracts version from 'ruff>=0.14,<2.0,!=1.5.0'", () => { |
| 51 | + const result = findRuffVersionInSpec("ruff>=0.14,<2.0,!=1.5.0"); |
| 52 | + expect(result).toBe(">=0.14,<2.0,!=1.5.0"); |
| 53 | + expect(warning).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns undefined for non-ruff dependencies", () => { |
| 57 | + const result = findRuffVersionInSpec("another-dep==0.1.6"); |
| 58 | + expect(result).toBeUndefined(); |
| 59 | + expect(info).not.toHaveBeenCalled(); |
| 60 | + expect(warning).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("strips trailing backslash", () => { |
| 64 | + const result = findRuffVersionInSpec("ruff==0.9.3 \\"); |
| 65 | + expect(result).toBe("0.9.3"); |
| 66 | + expect(info).toHaveBeenCalledWith( |
| 67 | + "Found ruff version in requirements file: 0.9.3", |
| 68 | + ); |
| 69 | + expect(warning).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("strips environment markers and warns", () => { |
| 73 | + const result = findRuffVersionInSpec( |
| 74 | + 'ruff>=0.14 ; python_version >= "3.11"', |
| 75 | + ); |
| 76 | + expect(result).toBe(">=0.14"); |
| 77 | + expect(info).toHaveBeenCalledWith( |
| 78 | + "Found ruff version in requirements file: >=0.14", |
| 79 | + ); |
| 80 | + expect(warning).toHaveBeenCalledWith( |
| 81 | + "Environment markers are ignored. ruff is a standalone tool that works independently of Python version.", |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it("handles whitespace", () => { |
| 86 | + const result = findRuffVersionInSpec(" ruff >=0.14 "); |
| 87 | + expect(result).toBe(">=0.14"); |
| 88 | + expect(warning).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns undefined for empty strings", () => { |
| 92 | + const result = findRuffVersionInSpec(""); |
| 93 | + expect(result).toBeUndefined(); |
| 94 | + expect(info).not.toHaveBeenCalled(); |
| 95 | + expect(warning).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe("getRuffVersionFromFile", () => { |
| 100 | + it("reads the version from requirements.txt", () => { |
| 101 | + const result = getRuffVersionFromFile( |
| 102 | + "__tests__/fixtures/requirements.txt", |
| 103 | + ); |
| 104 | + expect(result).toBe("0.9.0"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("reads the version from requirements files with hashes", () => { |
| 108 | + const result = getRuffVersionFromFile( |
| 109 | + "__tests__/fixtures/requirements-with-hash.txt", |
| 110 | + ); |
| 111 | + expect(result).toBe("0.9.0"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("reads the version from pyproject.toml dependencies", () => { |
| 115 | + const result = getRuffVersionFromFile( |
| 116 | + "__tests__/fixtures/pyproject.toml", |
| 117 | + ); |
| 118 | + expect(result).toBe("0.9.3"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("reads the version from Poetry dependencies", () => { |
| 122 | + const result = getRuffVersionFromFile( |
| 123 | + "__tests__/fixtures/pyproject-dependency-poetry-project/pyproject.toml", |
| 124 | + ); |
| 125 | + expect(result).toBe("~0.8.2"); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments