mirror of
https://github.com/octoleo/hosts.git
synced 2024-11-11 15:50:58 +00:00
Compare commits
190 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
8916c43efa | ||
|
9ea716f861 | ||
|
9a929dc70f | ||
|
80699292a4 | ||
|
06ad3e8deb | ||
|
c74671613b | ||
|
0201480ffb | ||
|
0ce875c210 | ||
|
6a58eb693d | ||
|
245188025a | ||
|
39a182b47e | ||
|
bd7edba09b | ||
|
5049abe817 | ||
|
5c87ad62b0 | ||
|
ca51f3f742 | ||
|
ade1109cba | ||
|
3c383581c5 | ||
|
2346f6f814 | ||
|
0715b9ec66 | ||
|
42a2a9f3d4 | ||
|
c11f22e558 | ||
|
0d5f41537d | ||
|
0a98202f84 | ||
|
040593bd51 | ||
|
0b79763874 | ||
|
d6e3a8a128 | ||
|
0fa9b240c6 | ||
|
83bb517312 | ||
|
d396c5eefb | ||
|
8ab47640f0 | ||
|
c3be802faf | ||
|
768acb5d80 | ||
|
379f99f4e1 | ||
|
28fea3a599 | ||
|
bf65fbcad8 | ||
|
5d4a9dfb28 | ||
|
0bd86e533d | ||
|
a39c60aafa | ||
|
1da838faeb | ||
|
6e1d408ad5 | ||
|
c381c8bd98 | ||
|
426f2ed602 | ||
|
7596a2901f | ||
|
4e9ad74697 | ||
|
051e8b3840 | ||
|
f585447612 | ||
|
4238e8bd81 | ||
|
a64c20bdd8 | ||
|
5865f92977 | ||
|
2b81ea530f | ||
|
1e6af9b309 | ||
|
47b5d51fa3 | ||
|
d775353c9a | ||
|
73ecc3119e | ||
|
d242aed463 | ||
|
9bf7f782c9 | ||
|
f0ebb08cd4 | ||
|
4aeab14d2d | ||
|
952e2465b3 | ||
|
23d6a1c653 | ||
|
17b2615820 | ||
|
68b421f95a | ||
|
a09e7b2468 | ||
|
dfbc83e12c | ||
|
fff87648d4 | ||
|
8f6a700909 | ||
|
1d948837d0 | ||
|
8f2553b5a3 | ||
|
2f6290f3d4 | ||
|
16a672d52a | ||
|
16a95406fe | ||
|
5a7d0cca91 | ||
|
40be65b7da | ||
|
affd8d8657 | ||
|
6e751065de | ||
|
3e326dad13 | ||
|
6bde3438c6 | ||
|
ca43d92d85 | ||
|
bc06de7476 | ||
|
9dab3606c6 | ||
|
88030b8a51 | ||
|
5112c3e6a0 | ||
|
2f5f610e3c | ||
|
815def022e | ||
|
59bb99ea34 | ||
|
40b76d2e26 | ||
|
bc6b16c014 | ||
|
a67e4c997b | ||
|
4088591bb2 | ||
|
972dc9a752 | ||
|
95807387a1 | ||
|
952d848157 | ||
|
89b016999d | ||
|
91946962a6 | ||
|
886599394e | ||
|
45b39d1bc7 | ||
|
6159d3e35a | ||
|
3d65bccdb5 | ||
|
8cbbdc3550 | ||
|
9cbc6ddafb | ||
|
809bb45121 | ||
|
638da3200e | ||
|
cd3d9ef2b5 | ||
|
b7394fe2d5 | ||
|
b9a257f5d5 | ||
|
ace77c9926 | ||
|
56a27a040e | ||
|
e88cd0b16a | ||
|
7b8b4db54b | ||
|
16c7484c17 | ||
|
c41b0ada74 | ||
|
b8c4ec06b9 | ||
|
5bb8add6de | ||
|
33c5aa1c3c | ||
|
c14c1a5213 | ||
|
c23a00bc49 | ||
|
fb67d7b510 | ||
|
7fc3b994b4 | ||
|
ce605e8c0c | ||
|
d772e7c13e | ||
|
7301dc38f1 | ||
|
5d9f876b13 | ||
|
902c996b3b | ||
|
3a810cf384 | ||
|
5916a18908 | ||
|
b44b69f83d | ||
|
3ccfbb5bc1 | ||
|
f67a88af94 | ||
|
c864eff0ec | ||
|
925de9dca5 | ||
|
9f0c3d1773 | ||
|
e9a7ecd6a7 | ||
|
85c1b7d436 | ||
|
0f3c59bd86 | ||
|
0a39dfe693 | ||
|
d91a4406a7 | ||
|
92f0e97073 | ||
|
13171f4a01 | ||
|
916009280e | ||
|
e858faec05 | ||
|
c6b84931a4 | ||
|
75e68b0d17 | ||
|
091e1c3809 | ||
|
774505abc0 | ||
|
659bbb4755 | ||
|
0247826a41 | ||
|
09dad21311 | ||
|
4eed5251da | ||
|
ada8cb6019 | ||
|
ab2a193371 | ||
|
4be5105f67 | ||
|
bfbe08a65b | ||
|
a2cddd9466 | ||
|
4fae0d5526 | ||
|
c680c02007 | ||
|
a54793a9d0 | ||
|
fa9ba543f5 | ||
|
850f210a4f | ||
|
fe48de86f1 | ||
|
2db235f68b | ||
|
e10d116510 | ||
|
060306f28b | ||
|
6b72e51651 | ||
|
a475dc53aa | ||
|
1ed6504bc2 | ||
|
14bfc65821 | ||
|
197ab087e6 | ||
|
789a0797c2 | ||
|
b44f3b8fe0 | ||
|
500d0f9b7f | ||
|
af412412f2 | ||
|
899783e72d | ||
|
5ee732006d | ||
|
647c4329c9 | ||
|
1738ba9ad3 | ||
|
0c11a1939e | ||
|
dfcb37b3b6 | ||
|
79a5aa95fc | ||
|
7f3f6a95ab | ||
|
6da4b7475f | ||
|
d8f86743f4 | ||
|
661fa3b114 | ||
|
dd25af8c99 | ||
|
2a5a18cf85 | ||
|
cfeb032707 | ||
|
a5bb823bac | ||
|
746780fbbe | ||
|
80edd464b6 | ||
|
b242081ef1 | ||
|
17a4eb1890 |
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
github: xwmx
|
||||
custom: https://paypal.me/WilliamMelody
|
84
.github/workflows/tests.yml
vendored
Normal file
84
.github/workflows/tests.yml
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
###############################################################################
|
||||
# .github/workflows/tests.yml
|
||||
#
|
||||
# NOTE: GitHub Actions does not allocate a TTY, preventing detection of
|
||||
# piped input using `[[ -t 0 ]]`.
|
||||
#
|
||||
# More information:
|
||||
#
|
||||
# https://github.com/actions/runner/issues/241
|
||||
#
|
||||
# faketty is a GitHub action that uses work-arounds to provide a tty:
|
||||
#
|
||||
# https://github.com/marketplace/actions/faketty
|
||||
#
|
||||
# Scripts used by faketty for each platform:
|
||||
#
|
||||
# linux: `faketty`
|
||||
#
|
||||
# ```bash
|
||||
# #!/bin/bash
|
||||
#
|
||||
# script -q -e -c "$*"
|
||||
# ```
|
||||
#
|
||||
# win32: `faketty.ps1`
|
||||
#
|
||||
# ```posh
|
||||
# Invoke-Expression "$args"
|
||||
# ```
|
||||
#
|
||||
# darwin: `faketty`
|
||||
#
|
||||
# requires: `brew install expect`
|
||||
#
|
||||
# ```bash
|
||||
# #!/bin/bash
|
||||
#
|
||||
# unbuffer $*
|
||||
# ```
|
||||
###############################################################################
|
||||
|
||||
name: "hosts · Test Suite"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
push:
|
||||
branches: [ master ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
test-macos-11:
|
||||
name: "Test: macOS Big Sur 11.0"
|
||||
runs-on: macos-11.0
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: "Setup"
|
||||
run: |
|
||||
brew update
|
||||
brew install bats-core
|
||||
brew install expect
|
||||
- name: "Set $TERM=xterm"
|
||||
run: printf "TERM=xterm\\n" >> $GITHUB_ENV
|
||||
- name: "Run bats tests"
|
||||
run: unbuffer bats test
|
||||
test-ubuntu-latest:
|
||||
name: "Test: Ubuntu Latest"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: "Setup"
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install bats -y
|
||||
- name: "Install bats-core"
|
||||
run: |
|
||||
git clone https://github.com/bats-core/bats-core.git "${HOME}/bats-core" &&
|
||||
cd "${HOME}/bats-core"
|
||||
git checkout 2e2e5df6adf0b846b411b6b2f7bb654cbc3e2c4e
|
||||
sudo ./install.sh /usr/local
|
||||
- name: "Set $TERM=xterm"
|
||||
run: printf "TERM=xterm\\n" >> $GITHUB_ENV
|
||||
- name: "Run bats tests"
|
||||
run: script -q -e -c "bats test"
|
22
.shellcheckrc
Normal file
22
.shellcheckrc
Normal file
@ -0,0 +1,22 @@
|
||||
###############################################################################
|
||||
# .shellcheckrc
|
||||
#
|
||||
# https://github.com/koalaman/shellcheck
|
||||
# https://github.com/koalaman/shellcheck/wiki/Ignore
|
||||
###############################################################################
|
||||
|
||||
# Disable SC2183
|
||||
#
|
||||
# Running into this: https://github.com/koalaman/shellcheck/issues/1310
|
||||
# TODO: Check if resolved.
|
||||
#
|
||||
#https://github.com/koalaman/shellcheck/wiki/SC2183
|
||||
disable=SC2183
|
||||
|
||||
# Disable SC2206 and SC2207
|
||||
#
|
||||
# `IFS` and `noglob` are set.
|
||||
#
|
||||
# https://github.com/koalaman/shellcheck/wiki/SC2206
|
||||
# https://github.com/koalaman/shellcheck/wiki/SC2207
|
||||
disable=SC2206,SC2207
|
2
Makefile
2
Makefile
@ -3,6 +3,8 @@ PREFIX ?= /usr/local
|
||||
|
||||
install:
|
||||
install $(BIN) $(PREFIX)/bin
|
||||
./$(BIN) completions install
|
||||
|
||||
uninstall:
|
||||
rm -f $(PREFIX)/bin/$(BIN)
|
||||
./$(BIN) completions uninstall
|
||||
|
504
README.md
504
README.md
@ -1,3 +1,5 @@
|
||||
[![Build Status](https://img.shields.io/github/actions/workflow/status/xwmx/hosts/tests.yml?branch=master)](https://github.com/xwmx/hosts/actions)
|
||||
|
||||
__ __
|
||||
/ /_ ____ _____/ /______
|
||||
/ __ \/ __ \/ ___/ __/ ___/
|
||||
@ -6,7 +8,15 @@
|
||||
|
||||
# Hosts
|
||||
|
||||
A command line program for managing hosts file entries.
|
||||
`hosts` is a command line program for managing
|
||||
[hosts file](https://en.wikipedia.org/wiki/Hosts_\(file\)) entries.
|
||||
|
||||
`hosts` works with existing hosts files and entries, making it easier to add,
|
||||
remove, comment, and search hosts file entries using simple, memorable
|
||||
commands.
|
||||
|
||||
`hosts` is designed to be lightweight, easy to use, and contained in a
|
||||
single, portable script that can be `curl`ed into any environment.
|
||||
|
||||
## Installation
|
||||
|
||||
@ -15,34 +25,314 @@ A command line program for managing hosts file entries.
|
||||
To install with [Homebrew](http://brew.sh/):
|
||||
|
||||
```bash
|
||||
brew tap alphabetum/taps && brew install alphabetum/taps/hosts
|
||||
brew tap xwmx/taps
|
||||
brew install hosts
|
||||
```
|
||||
|
||||
### npm
|
||||
|
||||
To install with [npm](https://www.npmjs.com/package/hosts.sh):
|
||||
|
||||
```bash
|
||||
npm install --global hosts.sh
|
||||
```
|
||||
|
||||
### bpkg
|
||||
|
||||
To install with [bpkg](http://www.bpkg.io/):
|
||||
To install with [bpkg](https://github.com/bpkg/bpkg):
|
||||
|
||||
```bash
|
||||
bpkg install alphabetum/hosts
|
||||
bpkg install xwmx/hosts
|
||||
```
|
||||
|
||||
### Make
|
||||
|
||||
To install with [Make](https://en.wikipedia.org/wiki/Make_(software)),
|
||||
clone this repository, navigate to the clone's root directory, and run:
|
||||
|
||||
```bash
|
||||
sudo make install
|
||||
```
|
||||
|
||||
### Manual
|
||||
|
||||
To install manually, simply add the `hosts` script to your `$PATH`. If
|
||||
you already have a `~/bin` directory, you can use the following command:
|
||||
To install as an administrator, copy and paste one of the following multi-line
|
||||
commands:
|
||||
|
||||
```bash
|
||||
curl -L https://raw.github.com/alphabetum/hosts/master/hosts \
|
||||
-o ~/bin/hosts && chmod +x ~/bin/hosts
|
||||
# install using wget
|
||||
sudo wget https://raw.github.com/xwmx/hosts/master/hosts -O /usr/local/bin/hosts &&
|
||||
sudo chmod +x /usr/local/bin/hosts &&
|
||||
sudo hosts completions install
|
||||
|
||||
# install using curl
|
||||
sudo curl -L https://raw.github.com/xwmx/hosts/master/hosts -o /usr/local/bin/hosts &&
|
||||
sudo chmod +x /usr/local/bin/hosts &&
|
||||
sudo hosts completions install
|
||||
```
|
||||
|
||||
###### User-only Installation
|
||||
|
||||
To install with just user permissions, simply add the `hosts` script to your
|
||||
`$PATH`. If you already have a `~/bin` directory, for example, you can use
|
||||
one of the following commands:
|
||||
|
||||
```bash
|
||||
# download with wget
|
||||
wget https://raw.github.com/xwmx/hosts/master/hosts -O ~/bin/hosts && chmod +x ~/bin/hosts
|
||||
|
||||
# download with curl
|
||||
curl -L https://raw.github.com/xwmx/hosts/master/hosts -o ~/bin/hosts && chmod +x ~/bin/hosts
|
||||
```
|
||||
|
||||
Installing with just user permissions doesn't install the completions, but
|
||||
`hosts` works without them. If you have `sudo` access and want to install the
|
||||
completion scripts, run the following command:
|
||||
|
||||
```bash
|
||||
sudo hosts completions install
|
||||
```
|
||||
|
||||
### Arch Linux
|
||||
|
||||
A package for Arch users is also
|
||||
[available in the AUR](https://aur.archlinux.org/packages/hosts/).
|
||||
|
||||
### Tab Completion
|
||||
|
||||
Bash and Zsh tab completion is enabled when `hosts` is installed using
|
||||
Homebrew, npm, bpkg, or Make. If you are installing `hosts` manually,
|
||||
[completion can be enabled with a few commands](etc/README.md).
|
||||
|
||||
## Usage
|
||||
|
||||
### Listing Entries
|
||||
|
||||
`hosts` with no arguments lists the entries in the system's hosts file:
|
||||
|
||||
```bash
|
||||
> hosts
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
```
|
||||
|
||||
`hosts` called with a string or regular expression will search for entries
|
||||
that match.
|
||||
|
||||
```bash
|
||||
> hosts localhost
|
||||
127.0.0.1 localhost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
|
||||
> hosts '\d\d\d'
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
```
|
||||
|
||||
### Adding Entries
|
||||
|
||||
To add an entry, use `hosts add`:
|
||||
|
||||
```bash
|
||||
> hosts add 127.0.0.1 example.com
|
||||
Added:
|
||||
127.0.0.1 example.com
|
||||
```
|
||||
|
||||
Run `hosts` or `hosts list` to see the new entry in the list:
|
||||
|
||||
```bash
|
||||
> hosts
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
127.0.0.1 example.com
|
||||
```
|
||||
|
||||
### Removing Entries
|
||||
|
||||
To remove an entry, use `hosts remove`, which can take an IP
|
||||
address, domain, or regular expression:
|
||||
|
||||
```bash
|
||||
> hosts remove example.com
|
||||
Removing the following entries:
|
||||
127.0.0.1 example.com
|
||||
Are you sure you want to proceed? [y/N] y
|
||||
Removed:
|
||||
127.0.0.1 example.com
|
||||
```
|
||||
|
||||
### Blocking and Unblocking Domains
|
||||
|
||||
`hosts` provides easy commands for blocking and unblocking domains with IPv4
|
||||
and IPv6 entries:
|
||||
|
||||
```bash
|
||||
> hosts block example.com
|
||||
Added:
|
||||
127.0.0.1 example.com
|
||||
Added:
|
||||
fe80::1%lo0 example.com
|
||||
Added:
|
||||
::1 example.com
|
||||
|
||||
> hosts unblock example.com
|
||||
Removed:
|
||||
127.0.0.1 example.com
|
||||
Removed:
|
||||
fe80::1%lo0 example.com
|
||||
Removed:
|
||||
::1 example.com
|
||||
```
|
||||
|
||||
### Enabling / Disabling Entries
|
||||
|
||||
All entries are enabled by default. Disabiling an entry comments it out
|
||||
so it has no effect, but remains in the hosts file ready to be enabled
|
||||
again.
|
||||
|
||||
```bash
|
||||
> hosts
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
127.0.0.1 example.com
|
||||
|
||||
> hosts disable example.com
|
||||
Disabling:
|
||||
127.0.0.1 example.com
|
||||
|
||||
> hosts
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
|
||||
Disabled:
|
||||
---------
|
||||
127.0.0.1 example.com
|
||||
|
||||
> hosts enable example.com
|
||||
Enabling:
|
||||
127.0.0.1 example.com
|
||||
|
||||
> hosts
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
127.0.0.1 example.com
|
||||
```
|
||||
|
||||
### Backups
|
||||
|
||||
Create backups of your hosts file with `hosts backups create`:
|
||||
|
||||
```bash
|
||||
> hosts backups create
|
||||
Backed up to /etc/hosts--backup-20200101000000
|
||||
```
|
||||
|
||||
List your backups with `hosts backups`. If you have existing hosts file
|
||||
backups, `hosts` will include them:
|
||||
|
||||
```bash
|
||||
> hosts backups
|
||||
hosts--backup-20200101000000
|
||||
hosts.bak
|
||||
```
|
||||
|
||||
`hosts backups compare` will open your hosts file with `diff`:
|
||||
|
||||
```bash
|
||||
> hosts backups compare hosts--backup-20200101000000
|
||||
--- /etc/hosts 2020-01-01 00:00:00.000000000
|
||||
+++ /etc/hosts--backup-20200101000000 2020-01-01 00:00:00.000000000
|
||||
@@ -8,3 +8,4 @@
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
+127.0.0.1 example.com
|
||||
```
|
||||
|
||||
View a backup with `hosts backups show`:
|
||||
|
||||
```bash
|
||||
> hosts backups show hosts--backup-20200101000000
|
||||
##
|
||||
# Host Database
|
||||
#
|
||||
# localhost is used to configure the loopback interface
|
||||
# when the system is booting. Do not change this entry.
|
||||
##
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
127.0.0.1 example.com
|
||||
```
|
||||
|
||||
Restore a backup with `hosts backups restore`. Before a backup is
|
||||
restored, a new one is created to avoid data loss:
|
||||
|
||||
```bash
|
||||
> hosts backups restore hosts--backup-20200101000000
|
||||
Backed up to /etc/hosts--backup-20200102000001
|
||||
Restored from backup: hosts--backup-20200101000000
|
||||
```
|
||||
|
||||
### Viewing and Editing `/etc/hosts` Directly
|
||||
|
||||
`hosts file` prints the raw contents of `/etc/hosts`:
|
||||
|
||||
```bash
|
||||
> hosts file
|
||||
##
|
||||
# Host Database
|
||||
#
|
||||
# localhost is used to configure the loopback interface
|
||||
# when the system is booting. Do not change this entry.
|
||||
##
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
```
|
||||
|
||||
`hosts edit` opens `/etc/hosts` in your editor:
|
||||
|
||||
```bash
|
||||
> hosts edit
|
||||
```
|
||||
|
||||
### `--auto-sudo`
|
||||
|
||||
When the `--auto-sudo` flag is used, all write operations that require
|
||||
`sudo` will automatically rerun the command using `sudo` when the current user
|
||||
does not have write permissions for the hosts file.
|
||||
|
||||
To have this option always enabled, add the following line to your shell
|
||||
configuration (`.bashrc`, `.zshrc`, or similar):
|
||||
|
||||
```bash
|
||||
alias hosts="hosts --auto-sudo"
|
||||
```
|
||||
|
||||
## Help
|
||||
|
||||
```text
|
||||
Usage:
|
||||
hosts [<search string>]
|
||||
hosts add <ip> <hostname> [<comment>]
|
||||
hosts block <hostname>
|
||||
hosts backups [create | (compare | delete | restore | show) <filename>]
|
||||
hosts block <hostname>...
|
||||
hosts completions (check | install [-d | --download] | uninstall)
|
||||
hosts disable (<ip> | <hostname> | <search string>)
|
||||
hosts disabled
|
||||
hosts edit
|
||||
@ -52,8 +342,9 @@ Usage:
|
||||
hosts list [enabled | disabled | <search string>]
|
||||
hosts search <search string>
|
||||
hosts show (<ip> | <hostname> | <search string>)
|
||||
hosts subcommands [--raw]
|
||||
hosts remove (<ip> | <hostname> | <search string>) [--force]
|
||||
hosts unblock <hostname>
|
||||
hosts unblock <hostname>...
|
||||
hosts --auto-sudo
|
||||
hosts -h | --help
|
||||
hosts --version
|
||||
@ -79,9 +370,31 @@ For help with a particular command, try:
|
||||
hosts help <command name>
|
||||
```
|
||||
|
||||
## Commands
|
||||
## Subcommands
|
||||
|
||||
## `hosts`
|
||||
<p align="center">
|
||||
<a href="#hosts-1">(default)</a> •
|
||||
<a href="#hosts-add">add</a> •
|
||||
<a href="#hosts-backups">backups</a> •
|
||||
<a href="#hosts-block">block</a> •
|
||||
<a href="#hosts-completions">completions</a> •
|
||||
<a href="#hosts-disable">disable</a> •
|
||||
<a href="#hosts-disabled">disabled</a> •
|
||||
<a href="#hosts-edit">edit</a> •
|
||||
<a href="#hosts-enable">enable</a> •
|
||||
<a href="#hosts-enabled">enabled</a> •
|
||||
<a href="#hosts-file">file</a> •
|
||||
<a href="#hosts-help">help</a> •
|
||||
<a href="#hosts-list">list</a> •
|
||||
<a href="#hosts-remove">remove</a> •
|
||||
<a href="#hosts-search">search</a> •
|
||||
<a href="#hosts-show">show</a> •
|
||||
<a href="#hosts-subcommands">subcommands</a> •
|
||||
<a href="#hosts-unblock">unblock</a> •
|
||||
<a href="#hosts-version">version</a>
|
||||
</p>
|
||||
|
||||
### `hosts`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
@ -89,13 +402,13 @@ Usage:
|
||||
|
||||
Description:
|
||||
List the existing IP / hostname pairs, optionally limited to a specified
|
||||
state. When provided with a seach string, all matching enabled records will
|
||||
state. When provided with a seach string, all matching enabled entries will
|
||||
be printed.
|
||||
|
||||
Alias for `hosts list`
|
||||
```
|
||||
|
||||
## `hosts add`
|
||||
### `hosts add`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
@ -103,30 +416,78 @@ Usage:
|
||||
|
||||
Description:
|
||||
Add a given IP address and hostname pair, along with an optional comment.
|
||||
|
||||
Exit status:
|
||||
0 Entry successfully added.
|
||||
1 Invalid parameters or entry exists.
|
||||
```
|
||||
|
||||
### `hosts backups`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
hosts backups
|
||||
hosts backups create
|
||||
hosts backups compare <filename>
|
||||
hosts backups delete <filename>
|
||||
hosts backups restore <filename> [--skip-backup]
|
||||
hosts backups show <filename>
|
||||
|
||||
Subcommands:
|
||||
backups List available backups.
|
||||
backups create Create a new backup of the hosts file.
|
||||
backups compare Compare a backup file with the current hosts file.
|
||||
backups delete Delete the specified backup.
|
||||
backups restore Replace the contents of the hosts file with a
|
||||
specified backup. The hosts file is automatically
|
||||
backed up before being overwritten unless the
|
||||
'--skip-backup' flag is specified.
|
||||
backups show Show the contents of the specified backup file.
|
||||
|
||||
Description:
|
||||
Manage backups.
|
||||
|
||||
Exit status:
|
||||
0 Success.
|
||||
1 Invalid parameters or backup not found.
|
||||
```
|
||||
|
||||
### `hosts block`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
hosts block <hostname>
|
||||
hosts block <hostname>...
|
||||
|
||||
Description:
|
||||
Block a given hostname by adding new entries assigning it to `127.0.0.1`
|
||||
Block one or more hostnames by adding new entries assigned to `127.0.0.1`
|
||||
for IPv4 and both `fe80::1%lo0` and `::1` for IPv6.
|
||||
|
||||
Exit status:
|
||||
0 <hostname> successfully blocked.
|
||||
1 Invalid parameters or entry exists.
|
||||
```
|
||||
|
||||
### `hosts commands`
|
||||
#### Blocklists
|
||||
|
||||
- [jmdugan/blocklists](https://github.com/jmdugan/blocklists)
|
||||
- [notracking/hosts-blocklists](https://github.com/notracking/hosts-blocklists)
|
||||
|
||||
### `hosts completions`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
hosts commands [--raw]
|
||||
hosts completions (check | install [-d | --download] | uninstall)
|
||||
|
||||
Options:
|
||||
--raw Display the command list without formatting.
|
||||
-d, --download Download the completion scripts and install.
|
||||
|
||||
Description:
|
||||
Display the list of available commands.
|
||||
Manage completion scripts. For more information, visit:
|
||||
https://github.com/xwmx/hosts/blob/master/etc/README.md
|
||||
|
||||
Exit status:
|
||||
0 Completions successfully installed.
|
||||
1 Invalid parameters or other error.
|
||||
```
|
||||
|
||||
### `hosts disable`
|
||||
@ -136,8 +497,12 @@ Usage:
|
||||
hosts disable (<ip> | <hostname> | <search string>)
|
||||
|
||||
Description:
|
||||
Disable one or more records based on a given ip address, hostname, or
|
||||
Disable one or more entries based on a given ip address, hostname, or
|
||||
search string.
|
||||
|
||||
Exit status:
|
||||
0 Entry successfully disabled.
|
||||
1 Invalid parameters or entry not found.
|
||||
```
|
||||
|
||||
### `hosts disabled`
|
||||
@ -147,7 +512,11 @@ Usage:
|
||||
hosts disabled
|
||||
|
||||
Description:
|
||||
List all disabled records. This is an alias for `hosts list disabled`.
|
||||
List all disabled entries. This is an alias for `hosts list disabled`.
|
||||
|
||||
Exit status:
|
||||
0 One or more disabled entries found.
|
||||
1 Invalid parameters or no disabled entries found.
|
||||
```
|
||||
|
||||
### `hosts edit`
|
||||
@ -167,8 +536,12 @@ Usage:
|
||||
hosts enable (<ip> | <hostname> | <search string>)
|
||||
|
||||
Description:
|
||||
Enable one or more disabled records based on a given ip address, hostname,
|
||||
Enable one or more disabled entries based on a given ip address, hostname,
|
||||
or search string.
|
||||
|
||||
Exit status:
|
||||
0 Entry successfully enabled.
|
||||
1 Invalid parameters or entry not found.
|
||||
```
|
||||
|
||||
### `hosts enabled`
|
||||
@ -178,7 +551,11 @@ Usage:
|
||||
hosts enabled
|
||||
|
||||
Description:
|
||||
List all enabled records. This is an alias for `hosts list enabled`.
|
||||
List all enabled entries. This is an alias for `hosts list enabled`.
|
||||
|
||||
Exit status:
|
||||
0 One or more enabled entries found.
|
||||
1 Invalid parameters or no enabled entries found.
|
||||
```
|
||||
|
||||
### `hosts file`
|
||||
@ -209,8 +586,12 @@ Usage:
|
||||
|
||||
Description:
|
||||
List the existing IP / hostname pairs, optionally limited to a specified
|
||||
state. When provided with a seach string, all matching enabled records will
|
||||
state. When provided with a seach string, all matching enabled entries will
|
||||
be printed.
|
||||
|
||||
Exit status:
|
||||
0 One or more matching entries found.
|
||||
1 Invalid parameters or entry not found.
|
||||
```
|
||||
|
||||
### `hosts remove`
|
||||
@ -224,9 +605,13 @@ Options:
|
||||
--force Skip the confirmation prompt.
|
||||
|
||||
Description:
|
||||
Remove one or more records based on a given IP address, hostname, or search
|
||||
string. If an IP and hostname are both provided, only records matching the
|
||||
Remove one or more entries based on a given IP address, hostname, or search
|
||||
string. If an IP and hostname are both provided, only entries matching the
|
||||
IP and hostname pair will be removed.
|
||||
|
||||
Exit status:
|
||||
0 Entry successfully removed.
|
||||
1 Invalid parameters or entry not found.
|
||||
```
|
||||
|
||||
### `hosts search`
|
||||
@ -237,6 +622,10 @@ Usage:
|
||||
|
||||
Description:
|
||||
Search entries for <search string>.
|
||||
|
||||
Exit status:
|
||||
0 One or more matching entries found.
|
||||
1 Invalid parameters or entry not found.
|
||||
```
|
||||
|
||||
### `hosts show`
|
||||
@ -247,16 +636,37 @@ Usage:
|
||||
|
||||
Description:
|
||||
Print entries matching a given IP address, hostname, or search string.
|
||||
|
||||
Exit status:
|
||||
0 One or more matching entries found.
|
||||
1 Invalid parameters or entry not found.
|
||||
```
|
||||
|
||||
### `hosts subcommands`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
hosts subcommands [--raw]
|
||||
|
||||
Options:
|
||||
--raw Display the subcommands list without formatting.
|
||||
|
||||
Description:
|
||||
Display the list of available subcommands.
|
||||
```
|
||||
|
||||
### `hosts unblock`
|
||||
|
||||
```text
|
||||
Usage:
|
||||
hosts unblock <hostname>
|
||||
hosts unblock <hostname>...
|
||||
|
||||
Description:
|
||||
Unblock a given hostname by removing its entries from the hosts file.
|
||||
Unblock one or more hostnames by removing the entries from the hosts file.
|
||||
|
||||
Exit status:
|
||||
0 <hostname> successfully unblocked.
|
||||
1 Invalid parameters or entry not found
|
||||
```
|
||||
|
||||
### `hosts version`
|
||||
@ -269,36 +679,16 @@ Description:
|
||||
Display the current program version.
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
### `--auto-sudo`
|
||||
|
||||
When specified, all write operations that require `sudo` will automatically
|
||||
rerun the command using `sudo` when the current user does not have write
|
||||
permissions for the hosts file.
|
||||
|
||||
To have this option always enabled, add the following line to your shell
|
||||
configuration (`.bashrc`, `.zshrc`, or similar):
|
||||
|
||||
```bash
|
||||
alias hosts="hosts --auto-sudo"
|
||||
```
|
||||
|
||||
### `-h` `--help`
|
||||
|
||||
Display help information.
|
||||
|
||||
### `--version`
|
||||
|
||||
Display version information.
|
||||
|
||||
## Tests
|
||||
|
||||
To run the test suite, install [Bats](https://github.com/sstephenson/bats) and
|
||||
To run the [test suite](test), install [Bats](https://github.com/sstephenson/bats) and
|
||||
run `bats test` in the project root directory.
|
||||
|
||||
## Acknowledgements
|
||||
---
|
||||
<p align="center">
|
||||
Copyright (c) 2015-present William Melody • See LICENSE for details.
|
||||
</p>
|
||||
|
||||
- https://gist.github.com/nddrylliog/1368532
|
||||
- https://gist.github.com/dfeyer/1369760
|
||||
- https://github.com/macmade/host-manager
|
||||
<p align="center">
|
||||
<a href="https://github.com/xwmx/hosts">github.com/xwmx/hosts</a>
|
||||
</p>
|
||||
|
113
etc/README.md
Normal file
113
etc/README.md
Normal file
@ -0,0 +1,113 @@
|
||||
# `hosts` Tab Completion
|
||||
|
||||
## Homebrew
|
||||
|
||||
Installing via Homebrew with `brew install xwmx/taps/hosts` will also
|
||||
install the completion scripts.
|
||||
|
||||
A one-time setup might be needed to [enable completion for all Homebrew
|
||||
programs](https://docs.brew.sh/Shell-Completion).
|
||||
|
||||
## npm, bpkg, Make
|
||||
|
||||
When `hosts` is installed with `npm`, `bpkg`, or Make, an install hook will
|
||||
check the environment and attempt to install completions. If it's successful,
|
||||
you should see a message similar to:
|
||||
|
||||
```bash
|
||||
Completion installed: /usr/local/etc/bash_completion.d/hosts
|
||||
Completion installed: /usr/local/share/zsh/site-functions/_hosts
|
||||
```
|
||||
|
||||
If completion is working after installing through any of these methods, then
|
||||
you don't need to do anything else.
|
||||
|
||||
## `hosts completions`
|
||||
|
||||
The `hosts completions` subcommand can be used for installing and uninstalling
|
||||
completion scripts. Depending on your configuration, you might need to use
|
||||
`sudo` to install completion scripts easily:
|
||||
|
||||
```bash
|
||||
> sudo hosts completions check
|
||||
Completion scripts not found.
|
||||
|
||||
> sudo hosts completions install
|
||||
Completion script installed: /usr/share/bash-completion/completions/hosts
|
||||
Completion script installed: /usr/local/share/zsh/site-functions/_hosts
|
||||
|
||||
> sudo hosts completions check
|
||||
Exists: /usr/share/bash-completion/completions/hosts
|
||||
Exists: /usr/local/share/zsh/site-functions/_hosts
|
||||
|
||||
> sudo hosts completions uninstall
|
||||
Completion script removed: /usr/share/bash-completion/completions/hosts
|
||||
Completion script removed: /usr/local/share/zsh/site-functions/_hosts
|
||||
```
|
||||
|
||||
If you installed `hosts` manually by downloading just the `hosts` script,
|
||||
the completion scripts won't be immediately available for
|
||||
`hosts completions install`. You can try installing the completions with
|
||||
the `--download` flag, which will get the latest version from GitHub:
|
||||
|
||||
```bash
|
||||
sudo hosts completions install --download
|
||||
```
|
||||
|
||||
`hosts completions` will try to determine the completion script directories
|
||||
from your environment. If `hosts completions` isn't able to install
|
||||
the completion scripts, you can try installing them manually.
|
||||
|
||||
## Manual Installation
|
||||
|
||||
### bash
|
||||
|
||||
#### Linux
|
||||
|
||||
On a current Linux OS (in a non-minimal installation), bash completion should
|
||||
be available.
|
||||
|
||||
Place the completion script in `/etc/bash_completion.d/`:
|
||||
|
||||
```bash
|
||||
sudo curl -L https://raw.githubusercontent.com/xwmx/hosts/master/hosts-completion.bash -o /etc/bash_completion.d/hosts
|
||||
```
|
||||
|
||||
#### macOS
|
||||
|
||||
If you aren't installing with homebrew, source the completion script in
|
||||
`.bash_profile`:
|
||||
|
||||
```sh
|
||||
if [[ -f /path/to/hosts-completion.bash ]]
|
||||
then
|
||||
source /path/to/hosts-completion.bash
|
||||
fi
|
||||
```
|
||||
|
||||
### zsh
|
||||
|
||||
Place the completion script in your `/path/to/zsh/completion` (typically
|
||||
`~/.zsh/completion/`):
|
||||
|
||||
```bash
|
||||
$ mkdir -p ~/.zsh/completion
|
||||
$ curl -L https://raw.githubusercontent.com/xwmx/hosts/master/hosts-completion.zsh > ~/.zsh/completion/_hosts
|
||||
```
|
||||
Include the directory in your `$fpath` by adding in `~/.zshrc`:
|
||||
|
||||
```bash
|
||||
fpath=(~/.zsh/completion $fpath)
|
||||
```
|
||||
|
||||
Make sure `compinit` is loaded or do it by adding in `~/.zshrc`:
|
||||
|
||||
```bash
|
||||
autoload -Uz compinit && compinit -i
|
||||
```
|
||||
|
||||
Then reload your shell:
|
||||
|
||||
```bash
|
||||
exec $SHELL -l
|
||||
```
|
@ -1,3 +1,5 @@
|
||||
#compdef hosts
|
||||
|
||||
__hosts_subcommands() {
|
||||
local _commands
|
||||
_commands=($(hosts commands --raw))
|
||||
@ -21,4 +23,4 @@ __hosts_subcommands() {
|
||||
fi
|
||||
}
|
||||
|
||||
compdef __hosts_subcommands hosts
|
||||
__hosts_subcommands "$@"
|
5
package-lock.json
generated
Normal file
5
package-lock.json
generated
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "hosts.sh",
|
||||
"version": "3.6.4",
|
||||
"lockfileVersion": 1
|
||||
}
|
37
package.json
37
package.json
@ -1,7 +1,38 @@
|
||||
{
|
||||
"name": "hosts",
|
||||
"version": "2.4.1",
|
||||
"name": "hosts.sh",
|
||||
"version": "3.6.4",
|
||||
"description": "A command line tool for managing hosts file entries.",
|
||||
"global": true,
|
||||
"install": "make install"
|
||||
"install": "make install",
|
||||
"bin": {
|
||||
"hosts": "./hosts"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bats test",
|
||||
"postinstall": "./hosts completions install",
|
||||
"preuninstall": "./hosts completions uninstall"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/xwmx/hosts.git"
|
||||
},
|
||||
"author": "William Melody",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/xwmx/hosts/issues"
|
||||
},
|
||||
"homepage": "https://github.com/xwmx/hosts#readme",
|
||||
"keywords": [
|
||||
"hosts",
|
||||
"shell",
|
||||
"command-line",
|
||||
"terminal",
|
||||
"hostname",
|
||||
"bash",
|
||||
"cli"
|
||||
],
|
||||
"dependencies": {}
|
||||
}
|
||||
|
121
test/add.bats
121
test/add.bats
@ -6,8 +6,8 @@ load test_helper
|
||||
|
||||
@test "\`add\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" add
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -15,15 +15,15 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`add\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" add
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts add <ip> <hostname> [<comment>]" ]]
|
||||
}
|
||||
@ -32,8 +32,8 @@ load test_helper
|
||||
|
||||
@test "\`add <ip>\` exits with status 1." {
|
||||
run "${_HOSTS}" add 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -41,15 +41,15 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip>\` prints help information." {
|
||||
run "${_HOSTS}" add 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Please include a hostname" ]]
|
||||
[[ "${lines[1]}" == "Usage:" ]]
|
||||
[[ "${lines[2]}" == " hosts add <ip> <hostname> [<comment>]" ]]
|
||||
@ -59,8 +59,8 @@ load test_helper
|
||||
|
||||
@test "\`add <ip> <hostname>\` exits with status 0." {
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -68,47 +68,102 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip> <hostname>\` prints feedback." {
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Added:" ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.com" ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip> <hostname>\` doesn't add duplicate entry." {
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
_modified="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
}
|
||||
|
||||
# `hosts add <ip> <hostname> [comment]` #######################################
|
||||
|
||||
@test "\`add <ip> <hostname> [comment]\` exits with status 0." {
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Comment.'
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.'
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip> <hostname> [comment]\` adds the entry to the hosts file." {
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Comment.'
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.'
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com # Comment." ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == \
|
||||
"0.0.0.0 example.com # Example multi-word comment." ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip> <hostname> [comment]\` doesn't add duplicate entry." {
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
_modified="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.'
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" != \
|
||||
"0.0.0.0 example.com # Example multi-word comment." ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip> <hostname> [comment]\` doesn't add duplicate commented entry." {
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.'
|
||||
}
|
||||
_modified="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.'
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
_compare '0.0.0.0 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_modified}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == \
|
||||
"0.0.0.0 example.com # Example multi-word comment." ]]
|
||||
}
|
||||
|
||||
@test "\`add <ip> <hostname> [comment]\` prints feedback." {
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Comment.'
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com 'Example multi-word comment.'
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Added:" ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.com # Comment." ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.com # Example multi-word comment." ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -120,8 +175,8 @@ load test_helper
|
||||
|
||||
@test "\`help add\` prints help information." {
|
||||
run "${_HOSTS}" help add
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts add <ip> <hostname> [<comment>]" ]]
|
||||
}
|
||||
|
257
test/backups.bats
Normal file
257
test/backups.bats
Normal file
@ -0,0 +1,257 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts backups` #############################################################
|
||||
|
||||
@test "\`backups\` with no backups and no arguments exits with status 0." {
|
||||
run "${_HOSTS}" backups
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`backups\` with backups and no arguments exits with status 0." {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`backups\` with no backups and no arguments prints message." {
|
||||
run "${_HOSTS}" backups
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${_HOSTS_TEMP_PATH}: '%s'\\n" "${_HOSTS_TEMP_PATH}"
|
||||
printf "\${HOSTS_PATH}: '%s'\\n" "${HOSTS_PATH}"
|
||||
_expected="\
|
||||
No backups found. Create a new backup:
|
||||
hosts backups create"
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
}
|
||||
|
||||
@test "\`backups\` with backups and no arguments prints list of backups." {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
sleep 1
|
||||
run "${_HOSTS}" backups create
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${_HOSTS_TEMP_PATH}: '%s'\\n" "${_HOSTS_TEMP_PATH}"
|
||||
printf "\${HOSTS_PATH}: '%s'\\n" "${HOSTS_PATH}"
|
||||
[[ "${lines[0]}" =~ hosts--backup- ]]
|
||||
[[ "${lines[1]}" =~ hosts--backup- ]]
|
||||
}
|
||||
|
||||
# `hosts backups create` ######################################################
|
||||
|
||||
@test "\`backups create\` exits with status 0." {
|
||||
run "${_HOSTS}" backups create
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`backups create\` creates backup." {
|
||||
run "${_HOSTS}" backups create
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
printf "\${_backup_path}: '%s'\\n" "${_backup_path}"
|
||||
[[ -e "${_backup_path}" ]]
|
||||
}
|
||||
|
||||
@test "\`backups create\` prints message." {
|
||||
run "${_HOSTS}" backups create
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${output} =~ 'Backed up to' ]]
|
||||
}
|
||||
|
||||
# `hosts backups compare` #####################################################
|
||||
|
||||
@test "\`backups compare\` with valid backup exits with status 1 and prints." {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups compare "${_backup_basename}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${lines[1]}: '%s'\\n" "${lines[1]}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
[[ "${lines[2]}" == '@@ -8,4 +8,3 @@' ]]
|
||||
}
|
||||
|
||||
@test "\`backups compare\` with missing backup exits with status 1" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups compare
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
[[ "${lines[0]}" =~ 'Usage' ]]
|
||||
}
|
||||
|
||||
@test "\`backups compare\` with invalid backup exits with status 1" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups compare "invalid-backup-name"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
[[ ${output} =~ 'Backup not found' ]]
|
||||
}
|
||||
|
||||
# `hosts backups delete` ######################################################
|
||||
|
||||
@test "\`backups delete\` with valid backup exits with status 0 and deletes backup" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
[[ -e "${_backup_path}" ]]
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups delete "${_backup_basename}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
[[ ! -e "${_backup_path}" ]]
|
||||
[[ ${output} =~ 'Backup deleted' ]]
|
||||
}
|
||||
|
||||
@test "\`backups delete\` with invalid backup exits with status 1" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups delete "invalid-backup-name"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
[[ -e "${_backup_path}" ]]
|
||||
[[ ${output} =~ 'Backup not found' ]]
|
||||
}
|
||||
|
||||
# `hosts backups restore` #####################################################
|
||||
|
||||
@test "\`backups restore\` with valid backup exits with status 0 and restores" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
sleep 1
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups restore "${_backup_basename}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${lines[0]}: '%s'\\n" "${lines[0]}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
[[ "${lines[0]}" =~ 'Backed up to' ]]
|
||||
[[ "${lines[1]}" =~ 'Restored from backup' ]]
|
||||
|
||||
_new_backup_path="$(echo "${lines[0]}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
|
||||
printf "\${_backup_path}: '%s'\\n" "${_backup_path}"
|
||||
printf "\${_new_backup_path}: '%s'\\n" "${_new_backup_path}"
|
||||
|
||||
_new_backup_content="$(cat "${_new_backup_path}")"
|
||||
_old_backup_content="$(cat "${_backup_path}")"
|
||||
_current_content="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
[[ "${_new_backup_content}" != "${_current_content}" ]]
|
||||
[[ "${_old_backup_content}" == "${_current_content}" ]]
|
||||
}
|
||||
|
||||
@test "\`backups restore --skip-backup\` with valid backup exits with status 0 and restores" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
_replaced_content="$(cat "${HOSTS_PATH}")"
|
||||
sleep 1
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups restore "${_backup_basename}" --skip-backup
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${lines[0]}: '%s'\\n" "${lines[0]}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
[[ "${lines[0]}" =~ 'Restored from backup' ]]
|
||||
|
||||
_old_backup_content="$(cat "${_backup_path}")"
|
||||
_current_content="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
[[ "${_replaced_content}" != "${_current_content}" ]]
|
||||
[[ "${_old_backup_content}" == "${_current_content}" ]]
|
||||
}
|
||||
|
||||
@test "\`backups restore\` with invalid backup exits with status 1" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups restore "invalid-backup-name"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
[[ ${output} =~ 'Backup not found' ]]
|
||||
}
|
||||
|
||||
# `hosts backups show` ########################################################
|
||||
|
||||
@test "\`backups show\` with valid backup exits with status 0 and prints." {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
[[ -e "${_backup_path:-}" ]]
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups show "${_backup_basename}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${lines[6]}: '%s'\\n" "${lines[6]}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
[[ "${lines[6]}" == '127.0.0.1 localhost' ]]
|
||||
}
|
||||
|
||||
@test "\`backups show\` with invalid backup exits with status 1" {
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups show "invalid-backup-name"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
[[ ${output} =~ 'Backup not found' ]]
|
||||
}
|
@ -2,12 +2,12 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts block` #################################################################
|
||||
# `hosts block` ###############################################################
|
||||
|
||||
@test "\`block\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" block
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -15,25 +15,25 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" block
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`block\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" block
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts block <hostname>" ]]
|
||||
[[ "${lines[1]}" == " hosts block <hostname>..." ]]
|
||||
}
|
||||
|
||||
# `hosts block <hostname>` #################################################
|
||||
# `hosts block <hostname>` ####################################################
|
||||
|
||||
@test "\`block <hostname>\` exits with status 0." {
|
||||
run "${_HOSTS}" block example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -41,24 +41,65 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" block example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "127.0.0.1 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`block <hostname>\` prints feedback." {
|
||||
run "${_HOSTS}" block example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Added:" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
[[ "${lines[2]}" == "Added:" ]]
|
||||
[[ "${lines[3]}" == "fe80::1%lo0 example.com" ]]
|
||||
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]example.com ]]
|
||||
[[ "${lines[4]}" == "Added:" ]]
|
||||
[[ "${lines[5]}" == "::1 example.com" ]]
|
||||
[[ "${lines[5]}" =~ \:\:1[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# `hosts block <hostname> <hostname2>` ########################################
|
||||
|
||||
@test "\`block <hostname> <hostname2>\` exits with status 0." {
|
||||
run "${_HOSTS}" block example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`block <hostname> <hostname2>\` adds entries to the hosts file." {
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" block example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
_compare '127.0.0.1 example2.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '14p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example2.com ]]
|
||||
}
|
||||
|
||||
@test "\`block <hostname> <hostname2>\` prints feedback." {
|
||||
run "${_HOSTS}" block example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Added:" ]]
|
||||
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
[[ "${lines[2]}" == "Added:" ]]
|
||||
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+example.com ]]
|
||||
[[ "${lines[4]}" == "Added:" ]]
|
||||
[[ "${lines[5]}" =~ \:\:1[[:space:]]+example.com ]]
|
||||
[[ "${lines[6]}" == "Added:" ]]
|
||||
[[ "${lines[7]}" =~ 127.0.0.1[[:space:]]+example2.com ]]
|
||||
[[ "${lines[8]}" == "Added:" ]]
|
||||
[[ "${lines[9]}" =~ fe80\:\:1\%lo0[[:space:]]+example2.com ]]
|
||||
[[ "${lines[10]}" == "Added:" ]]
|
||||
[[ "${lines[11]}" =~ \:\:1[[:space:]]+example2.com ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -70,8 +111,8 @@ load test_helper
|
||||
|
||||
@test "\`help block\` prints help information." {
|
||||
run "${_HOSTS}" help block
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts block <hostname>" ]]
|
||||
[[ "${lines[1]}" == " hosts block <hostname>..." ]]
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ load test_helper
|
||||
|
||||
@test "\`disable\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" disable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -15,15 +15,15 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" disable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`disable\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" disable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]]
|
||||
}
|
||||
@ -37,8 +37,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" disable 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -50,11 +50,11 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" disable 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.net ]]
|
||||
}
|
||||
|
||||
@test "\`disable <ip>\` disables all matches." {
|
||||
@ -65,11 +65,11 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" disable 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.net ]]
|
||||
}
|
||||
|
||||
@test "\`disable <ip>\` prints feedback." {
|
||||
@ -79,10 +79,10 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" disable 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Disabling:" ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# `hosts disable <hostname>` ##################################################
|
||||
@ -94,8 +94,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" disable example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -107,11 +107,11 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" disable example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.net ]]
|
||||
}
|
||||
|
||||
@test "\`disable <hostname>\` disables all matches." {
|
||||
@ -122,11 +122,11 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" disable example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.1 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled\:\ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`disable <hostname>\` prints feedback." {
|
||||
@ -136,10 +136,10 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" disable example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Disabling:" ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -151,8 +151,8 @@ load test_helper
|
||||
|
||||
@test "\`help disable\` prints help information." {
|
||||
run "${_HOSTS}" help disable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts disable (<ip> | <hostname> | <search string>)" ]]
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts disabled` #############################################################
|
||||
# `hosts disabled` ############################################################
|
||||
|
||||
@test "\`disabled\` with no arguments exits with status 0." {
|
||||
{
|
||||
@ -13,12 +13,12 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`disabled\` with no arguments prints list of disabled records." {
|
||||
@test "\`disabled\` with no arguments prints list of disabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -27,13 +27,20 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]]
|
||||
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`disabled\` exits with status 1 when no matching entries found." {
|
||||
run "${_HOSTS}" disabled
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
|
||||
@test "\`help disabled\` exits with status 0." {
|
||||
@ -43,8 +50,8 @@ load test_helper
|
||||
|
||||
@test "\`help disabled\` prints help information." {
|
||||
run "${_HOSTS}" help disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts disabled" ]]
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ load test_helper
|
||||
|
||||
@test "\`enable\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" enable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -15,15 +15,15 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" enable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`enable\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" enable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]]
|
||||
}
|
||||
@ -39,8 +39,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enable 127.0.0.2
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -55,12 +55,12 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" enable 127.0.0.2
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "127.0.0.2 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.net ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`enable <ip>\` enables all matches." {
|
||||
@ -74,8 +74,8 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" enable 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare \
|
||||
"${_original}" \
|
||||
"$(cat "${HOSTS_PATH}")"
|
||||
@ -89,12 +89,12 @@ load test_helper
|
||||
"'#disabled: 127.0.0.2 example.com'" \
|
||||
"'$(sed -n '13p' "${HOSTS_PATH}")'"
|
||||
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "#disabled: 127.0.0.2 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ \#disabled:\ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`disable <ip>\` prints feedback." {
|
||||
@test "\`enable <ip>\` prints feedback." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -103,10 +103,10 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enable 127.0.0.2
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Enabling:" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.2 example.com" ]]
|
||||
[[ "${lines[1]}" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# `hosts enable <hostname>` ###################################################
|
||||
@ -120,8 +120,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enable example.net
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -135,12 +135,12 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" enable example.net
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "127.0.0.2 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`enable <hostname>\` enables all matches." {
|
||||
@ -154,8 +154,8 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" enable example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare \
|
||||
"${_original}" \
|
||||
"$(cat "${HOSTS_PATH}")"
|
||||
@ -169,12 +169,12 @@ load test_helper
|
||||
"'127.0.0.2 example.com'" \
|
||||
"'$(sed -n '13p' "${HOSTS_PATH}")'"
|
||||
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "#disabled: 0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "127.0.0.2 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ \#disabled:\ 0.0.0.0[[:space:]]+example.net ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`disable <hostname>\` prints feedback." {
|
||||
@test "\`enable <hostname>\` prints feedback." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -183,10 +183,10 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enable example.net
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Enabling:" ]]
|
||||
[[ "${lines[1]}" == "0.0.0.0 example.net" ]]
|
||||
[[ "${lines[1]}" =~ 0.0.0.0[[:space:]]+example.net ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -198,8 +198,8 @@ load test_helper
|
||||
|
||||
@test "\`help enable\` prints help information." {
|
||||
run "${_HOSTS}" help enable
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts enable (<ip> | <hostname> | <search string>)" ]]
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`enabled\` with no arguments prints list of enabled records." {
|
||||
@test "\`enabled\` with no arguments prints list of enabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -27,13 +27,25 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
|
||||
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
|
||||
[[ "${lines[2]}" == "::1 localhost" ]]
|
||||
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
|
||||
[[ "${lines[4]}" == "127.0.0.2 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
|
||||
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
|
||||
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
|
||||
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
|
||||
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`enabled\` exits with status 1 when no matching entries found." {
|
||||
{
|
||||
run "${_HOSTS}" disable localhost
|
||||
run "${_HOSTS}" disable broadcasthost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" enabled
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -45,8 +57,8 @@ load test_helper
|
||||
|
||||
@test "\`help enabled\` prints help information." {
|
||||
run "${_HOSTS}" help enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts enabled" ]]
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ load test_helper
|
||||
|
||||
@test "\`help file\` prints help information." {
|
||||
run "${_HOSTS}" help file
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts file" ]]
|
||||
}
|
||||
|
1
test/fixtures/bin/hosts
vendored
Executable file
1
test/fixtures/bin/hosts
vendored
Executable file
@ -0,0 +1 @@
|
||||
#!/usr/bin/env bash
|
@ -10,9 +10,9 @@ load test_helper
|
||||
@test "\`hosts\` with no arguments prints enabled rules." {
|
||||
run "${_HOSTS}"
|
||||
[[ "${#lines[@]}" -eq 4 ]]
|
||||
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
|
||||
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
|
||||
[[ "${lines[2]}" == "::1 localhost" ]]
|
||||
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
|
||||
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
|
||||
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
|
||||
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
|
||||
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
|
||||
[[ "${lines[4]}" == "" ]]
|
||||
}
|
||||
|
137
test/list.bats
137
test/list.bats
@ -2,7 +2,7 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts list` #############################################################
|
||||
# `hosts list` ################################################################
|
||||
|
||||
@test "\`list\` exits with status 0." {
|
||||
{
|
||||
@ -13,12 +13,12 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`list\` prints lists of enabled and disabled records." {
|
||||
@test "\`list\` prints lists of enabled and disabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -27,18 +27,19 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
127.0.0.2 example.com
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
127.0.0.2 example.com
|
||||
|
||||
Disabled:
|
||||
0.0.0.0 example.com
|
||||
0.0.0.0 example.net"
|
||||
---------
|
||||
0.0.0.0 example.com
|
||||
0.0.0.0 example.net"
|
||||
_compare "'${_expected}'" "'${output}'"
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
}
|
||||
@ -54,12 +55,12 @@ Disabled:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`list enabled\` prints list of enabled records." {
|
||||
@test "\`list enabled\` prints list of enabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -68,13 +69,25 @@ Disabled:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
|
||||
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
|
||||
[[ "${lines[2]}" == "::1 localhost" ]]
|
||||
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
|
||||
[[ "${lines[4]}" == "127.0.0.2 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 127\.0\.0\.1[[:space:]]+localhost ]]
|
||||
[[ "${lines[1]}" =~ 255\.255\.255\.255[[:space:]]+broadcasthost ]]
|
||||
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
|
||||
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
|
||||
[[ "${lines[4]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`list enabled\` exits with status 1 when no matching entries found." {
|
||||
{
|
||||
run "${_HOSTS}" disable localhost
|
||||
run "${_HOSTS}" disable broadcasthost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list enabled
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# `hosts list disabled` #######################################################
|
||||
@ -88,12 +101,12 @@ Disabled:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`list disabled\` prints list of disabled records." {
|
||||
@test "\`list disabled\` prints list of disabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -102,13 +115,20 @@ Disabled:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]]
|
||||
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`list disabled\` exits with status 1 when no matching entries found." {
|
||||
run "${_HOSTS}" list disabled
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# `hosts list <search string>` ################################################
|
||||
|
||||
@test "\`list <search string>\` exits with status 0." {
|
||||
@ -119,12 +139,12 @@ Disabled:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`list <search string>\` prints list of matching records." {
|
||||
@test "\`list <search string>\` prints list of matching entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -132,13 +152,52 @@ Disabled:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.com ]]
|
||||
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example\.com ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`list <search string>\` prints entries with matching comments." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list "Comment"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
|
||||
@test "\`list <search string>\` prints disabled entries with matching comments." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.biz "Example Comment"
|
||||
run "${_HOSTS}" disable example.biz
|
||||
}
|
||||
|
||||
run "${_HOSTS}" list "Comment"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[4]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`list <search string>\` exits with status 1 when no matching entries found." {
|
||||
run "${_HOSTS}" list query-that-matches-no-entries
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
|
||||
@test "\`help list\` exits with status 0." {
|
||||
@ -148,8 +207,8 @@ Disabled:
|
||||
|
||||
@test "\`help list\` prints help information." {
|
||||
run "${_HOSTS}" help list
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts list [enabled | disabled | <search string>]" ]]
|
||||
}
|
||||
|
@ -2,12 +2,12 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts remove` #################################################################
|
||||
# `hosts remove` ##############################################################
|
||||
|
||||
@test "\`remove\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" remove
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -15,15 +15,15 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" remove
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`remove\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" remove
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]]
|
||||
}
|
||||
@ -38,8 +38,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" remove 127.0.0.3 --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -51,9 +51,9 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" remove 127.0.0.3 --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${output} == "No matching records found." ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${output} == "${_ERROR_PREFIX}No matching entries found." ]]
|
||||
}
|
||||
|
||||
# `hosts remove <ip> --force` #################################################
|
||||
@ -66,8 +66,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" remove 127.0.0.2 --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -80,12 +80,12 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" remove 127.0.0.2 --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`remove <ip>\` removes all matches." {
|
||||
@ -99,8 +99,8 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" remove 0.0.0.0 --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
##
|
||||
# Host Database
|
||||
@ -126,10 +126,10 @@ fe80::1%lo0 localhost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" remove 127.0.0.2 --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Removed:" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.2 example.com" ]]
|
||||
[[ "${lines[1]}" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# `hosts remove <hostname> --force` ###########################################
|
||||
@ -142,8 +142,8 @@ fe80::1%lo0 localhost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" remove example.com --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -156,10 +156,10 @@ fe80::1%lo0 localhost
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" remove example.com --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.net" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" =~ 0.0.0.0[[:space:]]+example.net ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "" ]]
|
||||
}
|
||||
|
||||
@ -174,8 +174,8 @@ fe80::1%lo0 localhost
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" remove example.com --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
##
|
||||
# Host Database
|
||||
@ -187,8 +187,8 @@ fe80::1%lo0 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
0.0.0.0 example.net
|
||||
#disabled: 0.0.0.0 example.dev"
|
||||
0.0.0.0 example.net
|
||||
#disabled: 0.0.0.0 example.dev"
|
||||
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]]
|
||||
@ -202,11 +202,11 @@ fe80::1%lo0 localhost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" remove example.com --force
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
Removed:
|
||||
0.0.0.0 example.com
|
||||
0.0.0.0 example.com
|
||||
127.0.0.2 example.com"
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
}
|
||||
@ -220,8 +220,8 @@ Removed:
|
||||
|
||||
@test "\`help remove\` prints help information." {
|
||||
run "${_HOSTS}" help remove
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts remove (<ip> | <hostname> | <search string>) [--force]" ]]
|
||||
}
|
||||
|
135
test/search.bats
135
test/search.bats
@ -2,7 +2,7 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts search` #############################################################
|
||||
# `hosts search` ##############################################################
|
||||
|
||||
@test "\`search\` with no arguments exits with status 1." {
|
||||
{
|
||||
@ -13,8 +13,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -27,16 +27,10 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
_expected="\
|
||||
Usage:
|
||||
hosts search <search string>
|
||||
|
||||
Description:
|
||||
Search entries for <search string>."
|
||||
_compare "'${_expected}'" "'${output}'"
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts search <search string>" ]]
|
||||
}
|
||||
|
||||
# `hosts search enabled` ######################################################
|
||||
@ -50,12 +44,12 @@ Description:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`search enabled\` prints list of enabled records." {
|
||||
@test "\`search enabled\` prints list of enabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -64,16 +58,28 @@ Description:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search enabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "127.0.0.1 localhost" ]]
|
||||
[[ "${lines[1]}" == "255.255.255.255 broadcasthost" ]]
|
||||
[[ "${lines[2]}" == "::1 localhost" ]]
|
||||
[[ "${lines[3]}" == "fe80::1%lo0 localhost" ]]
|
||||
[[ "${lines[4]}" == "127.0.0.2 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 127.0.0.1[[:space:]]+localhost ]]
|
||||
[[ "${lines[1]}" =~ 255.255.255.255[[:space:]]+broadcasthost ]]
|
||||
[[ "${lines[2]}" =~ \:\:1[[:space:]]+localhost ]]
|
||||
[[ "${lines[3]}" =~ fe80\:\:1\%lo0[[:space:]]+localhost ]]
|
||||
[[ "${lines[4]}" =~ 127.0.0.2[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# `hosts search disabled` #######################################################
|
||||
@test "\`search enabled\` exits with status 1 when no matching entries found." {
|
||||
{
|
||||
run "${_HOSTS}" disable localhost
|
||||
run "${_HOSTS}" disable broadcasthost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search enabled
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# `hosts search disabled` #####################################################
|
||||
|
||||
@test "\`search disabled\` exits with status 0." {
|
||||
{
|
||||
@ -84,12 +90,12 @@ Description:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`search disabled\` prints list of disabled records." {
|
||||
@test "\`search disabled\` prints list of disabled entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -98,13 +104,20 @@ Description:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search disabled
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`search disabled\` exits with status 1 when no matching entries found." {
|
||||
run "${_HOSTS}" search disabled
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# `hosts search <search string>` ################################################
|
||||
|
||||
@test "\`search <search string>\` exits with status 0." {
|
||||
@ -115,12 +128,12 @@ Description:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`search <search string>\` prints list of matching records." {
|
||||
@test "\`search <search string>\` prints list of matching entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -128,13 +141,51 @@ Description:
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "0.0.0.0 example.com" ]]
|
||||
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.com ]]
|
||||
[[ "${lines[1]}" =~ 127.0.0.1[[:space:]]+example.com ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`search <search string>\` prints entries with matching comments." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search "Comment"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`search <search string>\` prints disabled entries with matching comments." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.biz "Example Comment"
|
||||
run "${_HOSTS}" disable example.biz
|
||||
}
|
||||
|
||||
run "${_HOSTS}" search "Comment"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0.0.0.0[[:space:]]+example.net[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[3]}" =~ 127.0.0.1[[:space:]]+example.biz[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[4]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`search <search string>\` exits with status 1 when no matching entries found." {
|
||||
run "${_HOSTS}" search query-that-matches-no-entries
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
|
||||
@test "\`help search\` exits with status 0." {
|
||||
@ -144,8 +195,8 @@ Description:
|
||||
|
||||
@test "\`help search\` prints help information." {
|
||||
run "${_HOSTS}" help search
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts search <search string>" ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts search <search string>" ]]
|
||||
}
|
||||
|
@ -2,26 +2,26 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts show` ##############################################################
|
||||
# `hosts show` ################################################################
|
||||
|
||||
@test "\`show\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" show
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@test "\`show\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" show
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]]
|
||||
}
|
||||
|
||||
# `hosts show <ip>` #########################################################
|
||||
# `hosts show <no matching>` ##################################################
|
||||
|
||||
@test "\`show <ip>\` exits with status 0." {
|
||||
@test "\`show <query>\` with no matching entries with status 1." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -29,13 +29,17 @@ load test_helper
|
||||
run "${_HOSTS}" disable example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" show 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
run "${_HOSTS}" show bad-query
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
|
||||
[[ "${lines[0]}" =~ No\ matching\ entries ]]
|
||||
}
|
||||
|
||||
@test "\`enable <ip>\` shows all matches." {
|
||||
# `hosts show <ip>` ###########################################################
|
||||
|
||||
@test "\`show <ip>\` exits with status 0 and shows all matches." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -44,16 +48,17 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" show 0.0.0.0
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
|
||||
[[ "${lines[0]}" == "0.0.0.0 example.net" ]]
|
||||
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.net ]]
|
||||
[[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
# `hosts show <hostname>` #####################################################
|
||||
|
||||
@test "\`show <hostname>\` exits with status 0." {
|
||||
@test "\`show <hostname>\` exits with status 0 and shows all matches." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
@ -62,25 +67,63 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" show example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
|
||||
[[ "${lines[0]}" =~ 127\.0\.0\.2[[:space:]]+example.com ]]
|
||||
[[ "${lines[3]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
|
||||
}
|
||||
|
||||
@test "\`enable <hostname>\` shows all matches." {
|
||||
# `hosts show <search string>` ################################################
|
||||
|
||||
@test "\`show <search string>\` exits with status 0 and shows matching entries." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net
|
||||
run "${_HOSTS}" add 127.0.0.2 example.com
|
||||
run "${_HOSTS}" disable 0.0.0.0
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" show example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
|
||||
[[ "${lines[0]}" == "127.0.0.2 example.com" ]]
|
||||
[[ "${lines[1]}" == "disabled: 0.0.0.0 example.com" ]]
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example.com ]]
|
||||
[[ "${lines[1]}" =~ 127\.0\.0\.1[[:space:]]+example.com ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`show <search string>\` prints entries with matching comments." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" show "Comment"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${lines[0]}: '%s'\\n" "${lines[0]}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[2]}" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`show <search string>\` prints disabled entries with matching comments." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" add 0.0.0.0 example.net "Example Comment"
|
||||
run "${_HOSTS}" add 127.0.0.1 example.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.biz "Example Comment"
|
||||
run "${_HOSTS}" disable example.biz
|
||||
}
|
||||
|
||||
run "${_HOSTS}" show "Comment"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" =~ 0\.0\.0\.0[[:space:]]+example\.net[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[3]}" =~ 127\.0\.0\.1[[:space:]]+example\.biz[[:space:]]+\#\ Example\ Comment ]]
|
||||
[[ "${lines[4]}" == "" ]]
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -92,8 +135,8 @@ load test_helper
|
||||
|
||||
@test "\`help show\` prints help information." {
|
||||
run "${_HOSTS}" help show
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts show (<ip> | <hostname> | <search string>)" ]]
|
||||
}
|
||||
|
@ -12,16 +12,30 @@ setup() {
|
||||
# The location of the `hosts` script being tested.
|
||||
export _HOSTS="${BATS_TEST_DIRNAME}/../hosts"
|
||||
|
||||
export HOSTS_PATH="$(mktemp /tmp/hosts_test.XXXXXX)" || exit 1
|
||||
cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${HOSTS_PATH}"
|
||||
export _HOSTS_TEMP_DIR
|
||||
_HOSTS_TEMP_DIR="$(mktemp -d)"
|
||||
|
||||
export _HOSTS_TEMP_PATH
|
||||
_HOSTS_TEMP_PATH="${_HOSTS_TEMP_DIR}/hosts"
|
||||
|
||||
export _ERROR_PREFIX
|
||||
_ERROR_PREFIX="$(tput setaf 1)!$(tput sgr0) "
|
||||
|
||||
cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${_HOSTS_TEMP_PATH}"
|
||||
|
||||
export HOSTS_PATH="${_HOSTS_TEMP_PATH}"
|
||||
|
||||
# Use empty `hosts` script in environment to avoid depending on `hosts`
|
||||
# being available in `$PATH`.
|
||||
export PATH="${BATS_TEST_DIRNAME}/fixtures/bin:${PATH}"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
if [[ -n "${HOSTS_PATH}" ]] &&
|
||||
[[ -e "${HOSTS_PATH}" ]] &&
|
||||
[[ "${HOSTS_PATH}" =~ ^/tmp ]]
|
||||
if [[ -n "${_HOSTS_TEMP_PATH}" ]] &&
|
||||
[[ -e "${_HOSTS_TEMP_PATH}" ]] &&
|
||||
[[ "${_HOSTS_TEMP_PATH}" =~ ^/tmp/hosts_test ]]
|
||||
then
|
||||
rm "${HOSTS_PATH}"
|
||||
rm -f "${_HOSTS_TEMP_DIR}"/hosts_test.*
|
||||
fi
|
||||
}
|
||||
|
||||
@ -40,6 +54,6 @@ teardown() {
|
||||
_compare() {
|
||||
local _expected="${1:-}"
|
||||
local _actual="${2:-}"
|
||||
printf "expected:\n%s\n" "${_expected}"
|
||||
printf "actual:\n%s\n" "${_actual}"
|
||||
printf "expected:\\n%s\\n" "${_expected}"
|
||||
printf "actual:\\n%s\\n" "${_actual}"
|
||||
}
|
||||
|
@ -2,12 +2,12 @@
|
||||
|
||||
load test_helper
|
||||
|
||||
# `hosts unblock` #################################################################
|
||||
# `hosts unblock` #############################################################
|
||||
|
||||
@test "\`unblock\` with no arguments exits with status 1." {
|
||||
run "${_HOSTS}" unblock
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -15,20 +15,20 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" unblock
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_original}" ]]
|
||||
}
|
||||
|
||||
@test "\`unblock\` with no arguments prints help information." {
|
||||
run "${_HOSTS}" unblock
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts unblock <hostname>" ]]
|
||||
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
|
||||
}
|
||||
|
||||
# `hosts unblock <invalid>` ############################################
|
||||
# `hosts unblock <invalid>` ###################################################
|
||||
|
||||
@test "\`unblock <invalid> \` exits with status 1." {
|
||||
{
|
||||
@ -36,8 +36,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" unblock example.net
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
}
|
||||
|
||||
@ -47,12 +47,12 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" unblock example.net
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
[[ ${output} == "No matching records found." ]]
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${output} == "${_ERROR_PREFIX}No matching entries found." ]]
|
||||
}
|
||||
|
||||
# `hosts unblock <hostname>` ###########################################
|
||||
# `hosts unblock <hostname>` ##################################################
|
||||
|
||||
@test "\`unblock <hostname>\` exits with status 0." {
|
||||
{
|
||||
@ -62,8 +62,8 @@ load test_helper
|
||||
}
|
||||
|
||||
run "${_HOSTS}" unblock example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@ -76,10 +76,10 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" unblock example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
|
||||
}
|
||||
@ -93,8 +93,8 @@ load test_helper
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" unblock example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
##
|
||||
# Host Database
|
||||
@ -106,7 +106,7 @@ load test_helper
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
0.0.0.0 example.com
|
||||
0.0.0.0 example.com
|
||||
127.0.0.1 example.net"
|
||||
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
@ -121,15 +121,108 @@ fe80::1%lo0 localhost
|
||||
}
|
||||
|
||||
run "${_HOSTS}" unblock example.com
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
Removed:
|
||||
127.0.0.1 example.com
|
||||
Removed:
|
||||
fe80::1%lo0 example.com
|
||||
Removed:
|
||||
::1 example.com"
|
||||
::1 example.com"
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
}
|
||||
|
||||
# `hosts unblock <hostname> <hostname2>` ######################################
|
||||
|
||||
@test "\`unblock <hostname> <hostname2>\` exits with status 0." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" block example.com
|
||||
run "${_HOSTS}" block example2.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||
}
|
||||
|
||||
run "${_HOSTS}" unblock example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 0 ]]
|
||||
}
|
||||
|
||||
@test "\`unblock <hostname> <hostname2>\` updates the hosts file." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" block example.com
|
||||
run "${_HOSTS}" block example2.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||
}
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" unblock example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
|
||||
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
|
||||
}
|
||||
|
||||
@test "\`unblock <hostname> <hostname2>\` removes all matches." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" block example.com
|
||||
run "${_HOSTS}" block example2.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||
}
|
||||
_original="$(cat "${HOSTS_PATH}")"
|
||||
|
||||
run "${_HOSTS}" unblock example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
##
|
||||
# Host Database
|
||||
#
|
||||
# localhost is used to configure the loopback interface
|
||||
# when the system is booting. Do not change this entry.
|
||||
##
|
||||
127.0.0.1 localhost
|
||||
255.255.255.255 broadcasthost
|
||||
::1 localhost
|
||||
fe80::1%lo0 localhost
|
||||
0.0.0.0 example.com
|
||||
127.0.0.1 example.net"
|
||||
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
|
||||
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||
[[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]]
|
||||
}
|
||||
|
||||
@test "\`unblock <hostname> <hostname2>\` prints feedback." {
|
||||
{
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
run "${_HOSTS}" block example.com
|
||||
run "${_HOSTS}" block example2.com
|
||||
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||
}
|
||||
|
||||
run "${_HOSTS}" unblock example.com example2.com
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
_expected="\
|
||||
Removed:
|
||||
127.0.0.1 example.com
|
||||
Removed:
|
||||
fe80::1%lo0 example.com
|
||||
Removed:
|
||||
::1 example.com
|
||||
Removed:
|
||||
127.0.0.1 example2.com
|
||||
Removed:
|
||||
fe80::1%lo0 example2.com
|
||||
Removed:
|
||||
::1 example2.com"
|
||||
_compare "'${output}'" "'${_expected}'"
|
||||
diff <(echo "${output}" ) <(echo "${_expected}")
|
||||
[[ "${output}" == "${_expected}" ]]
|
||||
}
|
||||
|
||||
@ -142,8 +235,8 @@ Removed:
|
||||
|
||||
@test "\`help unblock\` prints help information." {
|
||||
run "${_HOSTS}" help unblock
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts unblock <hostname>" ]]
|
||||
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ load test_helper
|
||||
@test "\`hosts version\` prints a version number." {
|
||||
run "${_HOSTS}" version
|
||||
printf "'%s'" "${output}"
|
||||
echo "${output}" | grep -q '\d\+\.\d\+\.\d\+'
|
||||
echo "${output}" | grep -q '[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+'
|
||||
}
|
||||
|
||||
@test "\`hosts --version\` returns with 0 status." {
|
||||
@ -21,7 +21,7 @@ load test_helper
|
||||
@test "\`hosts --version\` prints a version number." {
|
||||
run "${_HOSTS}" --version
|
||||
printf "'%s'" "${output}"
|
||||
echo "${output}" | grep -q '\d\+\.\d\+\.\d\+'
|
||||
echo "${output}" | grep -q '[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+'
|
||||
}
|
||||
|
||||
# help ########################################################################
|
||||
@ -33,8 +33,8 @@ load test_helper
|
||||
|
||||
@test "\`help version\` prints help information." {
|
||||
run "${_HOSTS}" help version
|
||||
printf "\${status}: %s\n" "${status}"
|
||||
printf "\${output}: '%s'\n" "${output}"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ "${lines[0]}" == "Usage:" ]]
|
||||
[[ "${lines[1]}" == " hosts (version | --version)" ]]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user