<commit_before># frozen_string_literal: true

require "spec_helper"

describe Service::System do
  describe "#launchctl" do
    it "outputs launchctl command location" do
      expect(described_class.launchctl).to eq("/bin/launchctl")
    end
  end

  describe "#launchctl?" do
    it "outputs launchctl presence" do
      expect(described_class.launchctl?).to be(true)
    end
  end

  describe "#systemctl?" do
    it "outputs systemctl presence" do
      expect(described_class.systemctl?).to be(true)
    end
  end

  describe "#systemctl_scope" do
    it "outputs systemctl scope for user" do
      allow(described_class).to receive(:root?).and_return(false)
      expect(described_class.systemctl_scope).to eq("--user")
    end

    it "outputs systemctl scope for root" do
      allow(described_class).to receive(:root?).and_return(true)
      expect(described_class.systemctl_scope).to eq("--system")
    end
  end

  describe "#systemctl" do
    it "outputs systemctl command location" do
      expect(described_class.systemctl).to eq("/bin/systemctl")
    end
  end

  describe "#root?" do
    it "checks if the command is ran as root" do
      expect(described_class.root?).to be(false)
    end
  end

  describe "#user" do
    it "returns the current username" do
      expect(described_class.user).to eq(ENV.fetch("USER"))
    end
  end

  describe "#user_of_process" do
    it "returns the username for empty PID" do
      expect(described_class.user_of_process(nil)).to eq(ENV.fetch("USER"))
    end

    it "returns the PID username" do
      allow(Utils).to receive(:safe_popen_read).and_return <<~EOS
        USER
        user
      EOS
      expect(described_class.user_of_process(50)).to eq("user")
    end

    it "returns nil if unavailable" do
      allow(Utils).to receive(:safe_popen_read).and_return <<~EOS
        USER
      EOS
      expect(described_class.user_of_process(50)).to be_nil
    end
  end

  describe "#domain_target" do
    let(:service) do
      OpenStruct.new(name: "name", installed?: true, service_file: OpenStruct.new(file?: Pathname.new("/tmp_home/Library/LaunchAgents/homebrew.mysql.plist")))
    end

    it "returns the root domain target" do
      allow(described_class).to receive(:root?).and_return(true)
      expect(described_class.domain_target(service)).to match("system")
    end

    it "returns the current domain target for sessions with background support" do
      allow(described_class).to receive(:root?).and_return(false)
      allow(described_class).to receive(:domain_target_needs_background?).and_return(true)
      expect(described_class.domain_target(service)).to match(%r{user/(\d+)})
    end

    it "returns the current domain target" do
      allow(described_class).to receive(:root?).and_return(false)
      allow(described_class).to receive(:domain_target_needs_background?).and_return(false)
      expect(described_class.domain_target(service)).to match(%r{gui/(\d+)})
    end
  end

  describe "#boot_path" do
    it "macOS - returns the boot path" do
      allow(described_class).to receive(:launchctl?).and_return(true)
      expect(described_class.boot_path.to_s).to eq("/Library/LaunchDaemons")<commit_msg>Comply with style checks<commit_after># frozen_string_literal: true

require "spec_helper"

describe Service::System do
  describe "#launchctl" do
    it "outputs launchctl command location" do
      expect(described_class.launchctl).to eq("/bin/launchctl")
    end
  end

  describe "#launchctl?" do
    it "outputs launchctl presence" do
      expect(described_class.launchctl?).to be(true)
    end
  end

  describe "#systemctl?" do
    it "outputs systemctl presence" do
      expect(described_class.systemctl?).to be(true)
    end
  end

  describe "#systemctl_scope" do
    it "outputs systemctl scope for user" do
      allow(described_class).to receive(:root?).and_return(false)
      expect(described_class.systemctl_scope).to eq("--user")
    end

    it "outputs systemctl scope for root" do
      allow(described_class).to receive(:root?).and_return(true)
      expect(described_class.systemctl_scope).to eq("--system")
    end
  end

  describe "#systemctl" do
    it "outputs systemctl command location" do
      expect(described_class.systemctl).to eq("/bin/systemctl")
    end
  end

  describe "#root?" do
    it "checks if the command is ran as root" do
      expect(described_class.root?).to be(false)
    end
  end

  describe "#user" do
    it "returns the current username" do
      expect(described_class.user).to eq(ENV.fetch("USER"))
    end
  end

  describe "#user_of_process" do
    it "returns the username for empty PID" do
      expect(described_class.user_of_process(nil)).to eq(ENV.fetch("USER"))
    end

    it "returns the PID username" do
      allow(Utils).to receive(:safe_popen_read).and_return <<~EOS
        USER
        user
      EOS
      expect(described_class.user_of_process(50)).to eq("user")
    end

    it "returns nil if unavailable" do
      allow(Utils).to receive(:safe_popen_read).and_return <<~EOS
        USER
      EOS
      expect(described_class.user_of_process(50)).to be_nil
    end
  end

  describe "#domain_target" do
    let(:service) do
      OpenStruct.new(
        name: "name",
        installed?: true,
        service_file: OpenStruct.new(file?: Pathname.new("/tmp_home/Library/LaunchAgents/homebrew.mysql.plist")),
      )
    end

    it "returns the root domain target" do
      allow(described_class).to receive(:root?).and_return(true)
      expect(described_class.domain_target(service)).to match("system")
    end

    it "returns the current domain target for sessions with background support" do
      allow(described_class).to receive(:root?).and_return(false)
      allow(described_class).to receive(:domain_target_needs_background?).and_return(true)
      expect(described_class.domain_target(service)).to match(%r{user/(\d+)})
    end

    it "returns the current domain target" do
      allow(described_class).to receive(:root?).and_return(false)
      allow(described_class).to receive(:domain_target_needs_background?).and_return(false)
      expect(described_class.domain_target(service)).to match(%r{gui/(\d+)})
    end
  end

  describe "#boot_path" do
    it "macOS - returns the boot path" do
      allow(described_class).to receive(:launchctl?).and_return(true)
      expect(described_class.boot_path.to_s).to eq("/Library/LaunchDaemons")
{'fraction_non_alphanumeric': 0.1113013698630137, 'fraction_numerical': 0.0012453300124533001, 'mean_word_length': 3.59585121602289, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5297434', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1915, 'n_words': 469}